Exercise 32 - Sensors on LCD

1.      INTRODUCTION

Arduino can be used as a system of control and data acquisition. On this exercise, several sensors are going to be connected to an Arduino board to measure some parameter. You can choose any sensor, but this example uses the following sensors: Obstacle IR detector, potentiometer, Resistive force sensor. Furthermore, a simple menu is made using a LCD screen.

 

You can extrapolate this exercise to measure and control the parameters of a room or house to do a house automation system.



2.      HOW TO CONNECT?

Materials:

·         Arduino UNO board

·         Protoboard

·         Some wire Jumpers

·         Button

·         Force sensitive resistor

·         Potentiometer

·         IR obstacle sensor

·         2x 10K ohms resitors

Connections:

Pot: 5V, Pin A3, GND

Force sensor: 5V,Pin A2, 10K resistor, GND

Obstacle sensor: 5V, GND, Pin 3

Selection Button: 5V, Pin 2, 10K resistor, GND

EXERCISE_A


 

3.      PROGRAMMING

Do not forget to include necessary libraries!

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

 

// Set the LCD address to 0x27 for a 16 chars and 2 line display

LiquidCrystal_I2C lcd(0x27, 16, 2);

int antiReboundTime = 100;

int counter = 0;  //Variable to increment the counter

boolean lastState = LOW;    // Last state of the button

boolean currentState = LOW; // Current state of the button

const int buttonPin = 2;

int pot = A3;

int force = A2;

int obstacle = 3;

 

On setup the sensor has been declared as Inputs and the backlight on lcd is turned on. Also lcd and serial communication has been initialized.

void setup()

{

  pinMode(buttonPin, INPUT);

  pinMode(pot, INPUT);

  pinMode(force, INPUT);

  pinMode(obstacle, INPUT);

  // initialize the LCD

  lcd.begin();

 

  // Turn on the blacklight and print a message.

  lcd.backlight();

  lcd.print("HELLO!");

  delay(2000);

  Serial.begin(9600);  // Initialize Serial communication

}

 

debounce() function compares the current state of the button with the previous state and also return the value of the current state.

This function is adapted to use with a LCD screen if you use the anti rebound function on PUSH_BUTTON exercise, the characters will blink on LCD when they are printed.

 

boolean debounce(boolean last) // anti-rebound function

{

  boolean current = digitalRead(buttonPin);

 

  if (last != current)

  {

    current = digitalRead(buttonPin);

  }

 

  return current;

}

 

On loop, debounce function is called to eliminate rebound. On the other hand, if currentstate has a HIGH signal and lastState has a LOW signal the counter is incremented one by one because this condition means that the button is pressed.

Secondly, the condition changes when the counter is incremented, and it prints the value of the different sensors.

 

void loop()

{

 

  currentState = debounce(lastState); // Eliminates rebound

 

  if ( lastState == LOW && currentState == HIGH)

  {

    counter = counter + 1;           //Increments counter

    Serial.println(counter); //Prints the value on Serial monitor

  }

 

  lastState = currentState; // Update the state

This condition limits the counter to 1, 2 or 3.  

 

  if (counter < 1 || counter > 3) {

    counter = 1;

    lcd.setCursor(0, 1);

    lcd.print("                ");//Print 16 spaces to Erase row    

  }

  

  lcd.setCursor(0, 0);

  lcd.print("Selection: ");

  lcd.print(counter);

 

  if (counter == 1) {

    lcd.setCursor(0, 1);

    lcd.print("Pot Value=");

    lcd.print(analogRead(pot));

    lcd.print("                ");

  }

  

  if (counter == 2) {

    lcd.setCursor(0, 1);

    lcd.print("force Value=");

    lcd.print(analogRead(force));

    lcd.print("                ");

  }

  

  if (counter == 3) {

    lcd.setCursor(0, 1);

    lcd.print("Obstacle?->");

    if(digitalRead(obstacle)==0){

      lcd.print("Yes");

    }

    else{

      lcd.print("No");

    }

    lcd.print("                ");

  }

 

}


Última modificación: Tuesday, 21 de January de 2020, 10:33