Input Devices
April 15, 2024
Challenge
-
Group assignment:
- Probe an input device(s)’s analog levels and digital signals.
- Document your work on the group work page and reflect on your individual page what you learned.
-
Individual assignments:
- Measure something: add a sensor to a microcontroller board that you have designed and read it.
Group Assignment:
This week the assignment was fruitful as it helped me to understand the difference between analog and digital signals, the assignment give the chance to use both with the MCU I have previously build and how my MCU interacts with both sensors (I think all MCU’s will do the same as this is fixed among all MCU’s).
I understand now that analog sensors have many values between the GND and the VCC values where each have a specific value to the physical quantity that stimulate the sensor.
On the other hand the digital sensor consist of {two values} Low and High and each sensor have its own algorithm or way to represent the values through a library previously built.
For more information about the assignment please refer to this LINK.
Individual Assignment:
DHT 22
For this assignment I will be measuring {Temperature and Humidity} using DHT22 or AM2302 [Digital Sensor] Please find the Datasheet through this LINK.
DHT22 is a Digital sensor that measure temperature and humidity, For measuring temperature the sensor has thermister and for the humidity it has a capacitive humidity sensor. this sensor has a good precision (+- 0.5) but with a slow sampling rate (0.5 hz = 2 second ).
-
DHT22 pinout –> LINK
-
DHT22 Working principle –> LINK
-
So for thermister {Temperature} its a resistor its value change with temperature.
-
And for the Humidity {Capacitive} : Where there is a moisture holding substance between two electrodes so as the humidity changes the resistance of the moisture substance changes.
-
As This sensor is a digital sensor so the data is converted from analog to binary (discrete digital value).
Great documentation about the difference between analog and digital LINK
I Will be using the same board I have previously fabricated Which already have the DHT22 connected.
Below I will Just show the code and the output values as the sensor is already attached to the board.
-
For the code I will be using Arduino IDE.
-
I will Use also the DHT Library as this is a digital sensor.
- Then I will start by defining the sensor and its connection in addition to Including the library header file (DHT.h).
- Now I will initialize the serial link to use the serial monitor and I will also do the same for the DHT sensor.
- Now I will construct the loop function where I will create my variables to store the temperature and the humidity.
- Then I will create a check function if the reads failed so the MCU can save time and do it again.
- Then I will just print the data on the serial monitor.
- Now lets Upload and check.
#include <DHT.h>
#define PIN 8 // Where the DHT data pin is connected
#define TYPE DHT22 // DHT sensor Type.
DHT zaid(PIN,TYPE); // Constructor
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // Serial link with bud rate defined.
zaid.begin(); // DHT sensor with zaid name started.
}
void loop() {
// put your main code here, to run repeatedly:
delay(3000); // Take data every 3 seconds.
float T = zaid.readTemperature();// Read the temperature in celsius.
float TF = zaid.readTemperature(true);// Read Temperature in Fahrenheit
float H = zaid.readHumidity(); // Read Humidity.
// This sensor is know for failure let us create a check if reads failed.
if(isnan(T)||isnan(TF)||isnan(H)){
Serial.println("Faild to read");
return;
}
Serial.print("Humidity: ");
Serial.print(H);
Serial.print("% Temperature: ");
Serial.print(T);
Serial.print("°C ");
Serial.print(TF);
Serial.println("°F");
}
Note : This sensor needs 250 ms to give a considered data so its better to give time between readings.