Skip to content

Input devices

Hero Shot

Holder
Holder

Group assignment page.

Individual Assignment

On ‘Week 04 - Embedded programming’ I have designed a host board for my XIA0 ESP32 with a DHT22 Sensor, an LED and a push button. I have redesigned it with some SMD component and added an LDR. As usual, I have used arduino the arduino IDE to program It.

Holder

My Take away from interfacing an input device(s)

All sensor are not equal we need some times to add resistor for protection or for accurate reading Pulling up or pulling Down (avoiding parasite signal ....) these can be either pul up or pul down resistors. To find the required value Datasheet or research on internet can help us in finding the right value. Also sensor varies in way that we have Digital sensors and analog sensor. Both have to be interfaced to either digital port or analog port. Reading their values are then trough either analog reda function ou te digital read function.

Digital sensor

As Digital Sensor I have Used the DHT22 (Moisture and Temperature Sensor) Holder

The DHT22 sensor is a commonly used digital sensor for measuring temperature and humidity. It offers precise and reliable measurements. As a digital sensor, it converts data into digital signals, which makes it easier to use. It connects to microcontrollers via digital interfaces like One-Wire or I2C. The DHT22 is renowned for its accuracy, with a margin of error of ±0.5°C for temperature and ±2% for relative humidity. It is suitable for home automation, meteorology and environmental monitoring systems. It can measure temperatures from -40°C to +125°C and humidity levels from 0% to 100%. In summary, the DHT22 sensor is a popular digital sensor for accurate temperature and humidity measurement.

DHT 22 Datasheet

Analogue sensor

An analog sensor for Arduino measures a physical quantity and provides an analog output, such as a voltage or a current, which varies proportionally to the measured quantity. The Arduino board can convert this output to a digital value for use by the microcontroller (Arduino or XIAO board in this Case). This makes it possible to interface different analog sensors with Arduino to measure quantities such as temperature, light, pressure, etc. and make decisions based on those measurements.

Push buton:

I have also used a Push button as sensor. A push button can be used as an analog sensor by measuring the resistance or Voltage between its terminals. Depending on its state pressed or not. This which makes it possible to detect different levels of support. This variation can be used as an analog input for Arduino or other microcontrollers. It can serve as action trigger or motion end detector…

In my case I have Added a pull down resistor to it; and using its Hight state and low state reading.

Light Dependent Resistor (LDR)

A variable resistor can be used as an analog sensor as it resistance changes according the to the quantity of light. By adjusting the resistance, it is possible to obtain a voltage or current variation proportional to the quantity measured. This variation can be used as analog input for Arduino or other microcontrollers.

KiCad Screenshots

Holder

Holder

Holder

Holder

The Arduino Code

The Design board reads values from the previous sensors (The DHT22, The LDR, The Push Button.)

What the Code does

Basically it read and prints the value (From the DHT22, The LDR, The Push Button.) to the Serial Monitor.

Fist I have added the required library for DHT22 and declared variables. In the setup I have defined the pin modes for my tree inputs, initialized the serial communication and the DHT component.

In the previous function buttonSateChange() written on week 04 to monitor the button state I have commented the telegram bot lines so it prints the button state to the serial monitor. I have added that to the void loop with the reading and serial printing lines for the LDR and the DHT sensor.

Bellow the whole code.

The Code

#include "DHT.h"

#define DHTPIN D9

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

float temperatureC;
float temperatureF;
float humidity;
int btn;
int btnPIN = D1;
int led = D10;
int LDR = A0;
int lastButtonState = 0;


void setup()
{
  pinMode(led, OUTPUT);
  pinMode(LDR, INPUT);
  pinMode(btnPIN, INPUT);
  Serial.begin(9600);
  Serial.println(F("Ousiafb's XIO_ESP32"));

  dht.begin();

}

void loop()
{
  btn = digitalRead(btnPIN);
  Serial.print("Button State: ");
  Serial.println(btn);
  buttonSateChange();
  float LDRread = analogRead(LDR); //Reads the Value of LDR(light).
  Serial.print("LDR value is :");
  Serial.println(LDRread);

  humidity = dht.readHumidity();
  // Read temperature as Celsius (the default)
  temperatureC = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  temperatureF = dht.readTemperature(true);

  delay(500);
}

void buttonSateChange() {
  // compare the buttonState to its previous state
  if (btn != lastButtonState) {
    // if the state has changed, increment the counter
    if (btn == HIGH) {
      // if the current state is HIGH then the button went from off to on:
//      String msg = "Someone just opened the door";
//      bot.sendMessage(CHAT_ID, msg, "");
      Serial.println("opened");
    }
    else if (btn == LOW) {
      // if the current state is LOW then the button went from on to off:
//      String msg = "Someone just closed the door";
//      bot.sendMessage(CHAT_ID, msg, "");
      Serial.println("closed");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = btn;
}

Issues and Troubleshooting / Problems I encountered and I how I fixed them

  • As always when milling a board without probing and making a hight map in candle there were few not good isolation routing but we have made it and planned on making the probe and use it on the final project board milling.
  • Since I had already made the mistake of not mirroring my board for milling and took that previous mistake into account during this assignment.

File