Skip to content

11. Input Devices

PART ONE

Objective

for this week I’m going to drive a stepper motor and a pushbutton on the Xiao esp 32 C 3 using my designed electronic board.

components

  • Xiao esp 32 C3
  • stepper motor
  • button
  • cables

code

            //Jean-Nicaise fab 2023 
        #include <Stepper.h>
            double stepsPerRevolution = 2048;
            double stepsPerRevolution1 = 0;
            Stepper myStepper(stepsPerRevolution, 8, 10, 9, 7);     // Pin inversion to make the library work
            void setup() {

     myStepper.setSpeed(10);    
     Serial.begin(9600);
     pinMode(6, INPUT); 
        pinMode(5 , OUTPUT);
            }

                void loop() {

                        // 1 rotation counterclockwise:     
                        if(digitalRead(2) == HIGH){ 
                    Serial.println("counterclockwise");             
                        myStepper.step(stepsPerRevolution); 
                    digitalWrite(5, HIGH);

                }

                else {
                    Serial.println("counterclockwise");             
                        myStepper.step(stepsPerRevolution1);
                digitalWrite(5, LOW);
                    }

**the objective is to stop the motor rotating in any direction.

Wiring or cabling

movie

PART TWO

Objective

In this part we will use DHT11 to get temperature and humidity datas what we are going display on an LCD screen via I2C bus.

components

The components we need are :

  • Xiao esp 32 C3
  • LCD screen
  • DHT11 sensor
  • cables

code

below is the code we used

// FAB23 INPHB AKAFFOU JEAN NICAISE
double dewPoint(double celsius, double humidity)
{
        double A0= 373.15/(273.15 + celsius);
        double SUM = -7.90298 * (A0-1);
        SUM += 5.02808 * log10(A0);
        SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
        SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
        SUM += log10(1013.246);
        double VP = pow(10, SUM-3) * humidity;
        double T = log(VP/0.61078);   // temp var
        return (241.88 * T) / (17.558-T);
}


double dewPointFast(double celsius, double humidity)
{
        double a = 17.271;
        double b = 237.7;
        double temp = (a * celsius) / (b + celsius) + log(humidity/100);
        double Td = (b * temp) / (a - temp);
        return Td;
}

    #include <Wire.h> 
    #include <LiquidCrystal_I2C.h>
    #include <dht11.h>

dht11 DHT11;

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

#define DHT11PIN A2

void setup()
{
  Wire.setClock(400000);
  lcd.init();                      // initialize the lcd 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(0,0);
  for(int i=0; i<16; i++)
    {
      lcd.print("DHT11 TEST PROGRAM ");
      delay(300);
    }
  lcd.autoscroll();
  lcd.noAutoscroll();
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("LIBRARY VERSION: ");
  lcd.setCursor(0,1);
  lcd.print(DHT11LIB_VERSION);
  delay(1000);
  lcd.clear();

  int chk = DHT11.read(DHT11PIN);
  lcd.setCursor(2,0);
  lcd.print("Read sensor: ");
  switch (chk)
  {
    case DHTLIB_OK: 
                Serial.println("OK"); 
                break;
    case DHTLIB_ERROR_CHECKSUM: 
                lcd.setCursor(1,1);
                lcd.print("Checksum error"); 
                break;
    case DHTLIB_ERROR_TIMEOUT: 
                lcd.setCursor(1,1);
                lcd.print("Time out error"); 
                break;
    default: 
                lcd.setCursor(1,1);
                lcd.println("Unknown error"); 
                break;
  }
  delay(1500);
}


void loop()
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Humidity(%):");
  lcd.print((float)DHT11.humidity, 1);
  lcd.setCursor(0,1);
  lcd.print("Temperature:");
  lcd.print((float)DHT11.temperature, 1);
  delay(1000);
}

Wiring or Cabling

** The components connection **

dht11 lcdi2C 1602 xiao board
OUT - A2
GND GND GND
VCC VCC VCC
- SCL SCL
- SDA SDA

Link toward components description and use

lcdi2C dht11

Explaining code

    // FAB23 INPHB AKAFFOU JEAN NICAISE
double dewPoint(double celsius, double humidity)
{
        double A0= 373.15/(273.15 + celsius);
        double SUM = -7.90298 * (A0-1);
        SUM += 5.02808 * log10(A0);
        SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
        SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
        SUM += log10(1013.246);
        double VP = pow(10, SUM-3) * humidity;
        double T = log(VP/0.61078);   // temp var
        return (241.88 * T) / (17.558-T);
}


double dewPointFast(double celsius, double humidity)
{
        double a = 17.271;
        double b = 237.7;
        double temp = (a * celsius) / (b + celsius) + log(humidity/100);
        double Td = (b * temp) / (a - temp);
        return Td;
}
This part of code enable to display the temperature and and the display by calculating all prerequesites.

Next we included the libraries we used.

        #include <Wire.h> 
         #include <LiquidCrystal_I2C.h>
         #include <dht11.h>

dht11 DHT11;
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
Above we defined pins and what type of LCD we used :

LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display That means that we use 1602 lcd screen with its I2C address 0x27

In the setup we’ve initialized the lcd

 Wire.setClock(400000);
  lcd.init();                      // initialize the lcd 
  // Print a message to the LCD.
  lcd.backlight();

Then we displayed text. Fisrt text we displayed will sroll around the screen.

  lcd.setCursor(0,0);
  for(int i=0; i<16; i++)
    {
      lcd.print("DHT11 TEST PROGRAM ");
      delay(300);
    }
  lcd.autoscroll();
  lcd.noAutoscroll();
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("LIBRARY VERSION: ");
  lcd.setCursor(0,1);
  lcd.print(DHT11LIB_VERSION);
  delay(1000);
  lcd.clear();

  int chk = DHT11.read(DHT11PIN);
  lcd.setCursor(2,0);
  lcd.print("Read sensor: ");
  switch (chk)
  {
    case DHTLIB_OK: 
                Serial.println("OK"); //If the connexion is established print OK
                break;
    case DHTLIB_ERROR_CHECKSUM: 
                lcd.setCursor(1,1);
                lcd.print("Checksum error"); 
                break;
    case DHTLIB_ERROR_TIMEOUT: 
                lcd.setCursor(1,1);
                lcd.print("Time out error"); 
                break;
    default: 
                lcd.setCursor(1,1);
                lcd.println("Unknown error"); 
                break;
  }
  delay(1500);

In the loop we proceed to the main part of the code that will be execute. constantly.

Result


Last update: June 28, 2023