Skip to content

7. Input/Output devices

This week I worked on programming two inputs and one output devices using Arduino Microcontroller.

Input

DHT11 sensor

DHT11 sensor is humidity and temperature sensor. Humidity is calculated by measuring the electrical resistance between two electrodes. Temperature is measured using thermistor.

There are three pins. VCC for power supply to the device. Ground is the the reference point against which the VCC is compared. Signal sends results for both temperature and humidity as an electrical signal.

Library must be installed since it has neccessary functions that Arduino software uses to program and read data of DHT11 sensor.

Source

Original source code provided unexpected results because sensor need more time between each reading.

Code is adjusted a little bit. Pins are corrected and delay time is extended to 2 seconds instead of one.

#include <dht.h>

dht DHT;

#define DHT11_PIN A5

void setup(){
  Serial.begin(9600);
}

void loop(){
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  delay(2000);
}

KY-037 Sound detection sensor

This sensor measures the changes in the intensity of sound in the environment.

Source

Serial chart is chosen to give us better view for changing in sound intensity over time.

void setup() {
Serial.begin(9600); // setup serial
}
void loop() {
Serial.println(analogRead(A6));
delay(100);
}

Output

HW-479 RGB sensor

RGB light sensor consist of three led lights red, green and blue. All three colors range between 0 and 255 so, there are 256 values to choose from for each color. Lower values give less bright color while higher vales give brighter color.

Code example

int red_light_pin= A2;
int green_light_pin = A3;
int blue_light_pin = A1;
void setup() {
  pinMode(red_light_pin, OUTPUT);
  pinMode(green_light_pin, OUTPUT);
  pinMode(blue_light_pin, OUTPUT);
}
void loop() {
  RGB_color(255, 0, 0); // Red
  delay(5000);
  RGB_color(0, 255, 0); // Green
  delay(1000);
  RGB_color(0, 0, 255); // Blue
  delay(1000);
  RGB_color(0, 0, 0); // No color!
  delay(1000);
  RGB_color(255, 255, 255); // White
  delay(1000);
}
void RGB_color(int red_light_value, int green_light_value, int blue_light_value)
 {
  analogWrite(red_light_pin, red_light_value);
  analogWrite(green_light_pin, green_light_value);
  analogWrite(blue_light_pin, blue_light_value);
}

Input with output

To set standard for testing in casein adhesive project humidity and temperature of the atmosphere will be within a range. Temperature range is estimated to be between 20 and 25 degree celsius. While humidity range is between 50 and 60 percent.

Code example

#include <dht.h>
int red_light_pin= A2;
int green_light_pin = A3;
int blue_light_pin = A1;
dht DHT;

#define DHT11_PIN A5

void setup(){
  Serial.begin(9600);
   pinMode(red_light_pin, OUTPUT);
  pinMode(green_light_pin, OUTPUT);
  pinMode(blue_light_pin, OUTPUT);
}

void loop(){
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  delay(2000);
  if (DHT.temperature >= 20 and DHT.temperature <= 25 and DHT.humidity >=50 and DHT.humidity <=60)
  RGB_color(0, 255, 0); // green
  else RGB_color(255, 0, 0); // red
}

void RGB_color(int red_light_value, int green_light_value, int blue_light_value)
 {
  analogWrite(red_light_pin, red_light_value);
  analogWrite(green_light_pin, green_light_value);
  analogWrite(blue_light_pin, blue_light_value);
}

If temperature and humidity are within the range green color will appear.

Otherwise red color will appear.


Last update: August 16, 2021