Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views16 pages

Lab 2

This document outlines an exercise focused on working with various sensors and actuators, specifically controlling an LED with a button and reading data from a temperature-humidity sensor to display on an LCD. It provides detailed instructions on setting up hardware, writing code for Arduino IDE, and understanding the functionality of sensors and actuators. The exercise aims to enhance practical skills in electronics and programming related to sensor data handling and actuator control.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views16 pages

Lab 2

This document outlines an exercise focused on working with various sensors and actuators, specifically controlling an LED with a button and reading data from a temperature-humidity sensor to display on an LCD. It provides detailed instructions on setting up hardware, writing code for Arduino IDE, and understanding the functionality of sensors and actuators. The exercise aims to enhance practical skills in electronics and programming related to sensor data handling and actuator control.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Exercise #2

Working with different sensors and actuators. Button control LED.


Read data from temperature – humidity sensor and print to LCD display.

1. Target:
➢ Introduction to different sensors and actuators.
➢ To be able to control LED with buttons.
➢ To be able to read data from temperature – humidity sensor and print to LCD
display.
2. Introduction to different sensors and actuators
2.1. Sensors
Sensors are technical components for the qualitative or quantitative measure-
ment of certain chemical or physical variables and properties, for example,
temperature, light (intensity and color), acceleration, electricity, and so on. The
recorded measured values are usually converted into electronic signals. Currently,
we are already surrounded by sensors in many places. For example, modern
automobiles contain hundreds of sensors, for example, rain sensors for windshield
wiper systems, crash sensors for air bag release systems, and lane and parking-assist
sensors. Indeed, modern automobiles, some with far more than 200 sensors and a
few dozen microprocessors, constitute a good example of this. In fact, the ordinary
automobile is increasingly becoming one unified computerized object. In addition,
when a sensor is employed together with a processor (controller), a power supply,
and a unit for data transmission, this is referred to as a sensor node.
Examples of sensor categories include the following:
• Location: GPS, GLONASS, Galileo
• Biometric: fingerprint, iris, face
• Acoustic: microphone
• Environmental: temperature, humidity, pressure
• Motion: accelerometer, gyroscope
2.2. Actuators
Actuators convert electrical signals (e.g., commands emanating from the
control computer) into mechanical motion or other physical variables (e.g., pressure
or temperature), and thus actively intervene with the control system and/or set
variables. In the field of measurement and control engineering, actuators are the
signal-related counterparts to sensors. Types of actuators include hydraulic,
pneumatic, electric, mechanical, and piezoelectric. They convert signals or setting
and regulation specifications of a control into (mostly) mechanical work. A simple
example of this is the opening and closing of a valve, for example, in a heating
system or in the case of engine controls. The output of optical (via displays) or
acoustic signals can also be subsumed under actuators, since they can trigger an
effect in the real environment. In robotics, the term effector is often used as an
equivalent for actuators. Effectors allow a robot to grasp and manipulate objects, and
thus produce an effect. In a computerized world of things, actuators play an
increasingly important role in the realization of actions and effects as a counterpart
to the (previously) sensory-detected corresponding contexts. Actuators are a key
building block in more recent perceptions of the “Fourth Industrial Revolution” in
manufacturing as an Industry 4.0 conceptualization postulate.
2.3. Examples
Some of the sensors and actuators available in the laboratory are shown in the
table below.
SDA → D2
(GPIO4)
LCD 16x02 + LCD module
SCL → D1
(GPIO5)

DHT11 . Humidity and Temperature


Sensor Module

CZN-15E Mic based Sound Sensor


Module

Module Relay JQC3F-05VDC-C

Analog JoyStick

8pin 4x4 4*4 Matrix 16 Keys Button


Keypad
Potentiometer

MH real time clock module

Water sensor

Module HW-479 RGB Led

Matrix LED 8x8

4 Digit 7 Segment Displays


3461BS-1

Flame sensor
Buzzer

Switch (button)

Vibration sensor(SW-520D)

LED red blue green

Photoresistor

LM35 Temperature Sensor

180 degree SG90 servo motor

Stepper motor + Driver ULN2003


3. Working with different sensors and actuators
3.1. Control the LED lights by using a button
In lab_1, we have learned how to control the LED blink for every second. In
this Exercise, we will learn how to control the LED lights by using a button. It mean
when pressed the button, the LED was lighten and turn off when released.
Preparation:
• NodeMCU x1
• Push Button x1
• LED x1
• 10 kΩ Resistor x1
• 200 Ω Resistor x1
• Bread Board x1
• Jumper wire Male to Male
Let’s start! Setup the hardware according the connection diagram.

The Push Button is a electronic component that connects two points in a circuit
when you press it. Push buttons come in different shapes and sizes. But the internal
structure and working principle are the same for most push buttons. Push buttons
usually have four pins. But these pins are connected internally in pairs. So actually
you have two connections. When you press the button these two terminals will
connect together. And when you release the button these terminals will disconnect.
In the above image, you can see the schematic of a push button. It has two
terminals marked 1 and 2. When you press the button terminal 1 and 2 will connect
together.
Push Button connections: The first pin goes from one leg of the push button
through a pull-up resistor (here R2 = 10 kΩ) to the 3-5v supply. The second pin goes
from the corresponding leg of the push button to Ground (GND) pin. The third pin
connects to a Digital I/O pin (here pin D0-GPIO16) which reads the button's state.
LED connections: LED Anode (longer leg) is connected through a resistor (here
R1=220 Ω) to Digital I/O pin (here pin D6- GPIO12) and Cathode (shorter leg) to
ground (GND) pin.
Note! Information about the pin diagram of NodeMCU ESP8266 V3 is shown
in the figure below.
Write below code to your Arduino IDE, and choose the corresponding board
type and port type for project, upload the sketch to the NodeMCU.

After upload done, we will see the program running results: when the button
switch is pressed, LED light up; when the switch is released, the LED goes out (or
vice versa).
In this way, if we want to turn on the LED in this way, the hand cannot leave
button, it is not convenient. How to control the lights as normal, click on the light,
then press out? We can improve the program, which can realize the result that will
modify the program for the following code, and then uploaded to the NodeMCU
controller. The connection is same as above:
After the program is compiled and uploaded, you can achieve: click the button
to turn on the LED, and then press this button, the LED will be turned off.
3.2. Read data from DHT11 and show to LCD display
In this exercise, we will read data from temperature - humidity sensor DHT11
and use the LCD display to show information.
Preparation:
• NodeMCU
• Bread Board
• LCD 16x2 + LCD module
• Jumper wire Male to Male, Female to Female
• Humidity and Temperature Sensor Module DHT11.
DHT11 sensor connections: DHT11 sensor has been integrated in a single
circuit, you just need to connect the power wire (Vcc, GND) and signal wire (Signal)
to the ESP8266 board (here GPIO14). The connection diagram of DHT11 sensor
with ESP8266 is shown on the figure below.

LCD connections: We can wire LCD to the ESP8266 by following the next
schematic diagram. We use the ESP8266 default I2C pins (GPIO4 and GPIO5).

You can also use the following table as a reference.

I2C LCD ESP8266


GND GND
VCC VIN
SDA GPIO 4
SCL GPIO 5
Installing libraries for LCD and DHT11 sensor:
• Click here to download the LiquidCrystal_I2C library:
https://drive.google.com/file/d/11cBnDKaXLlStsqM8R8juEdsicCXCgL75/vi
ew?usp=sharing
Unzip the .zip file and move the LiquidCrystal_I2C folder to your Arduino IDE
installation “libraries” folder. (example C:\Users\...\Documents\Arduino\libraries.
You can see this URL in the File -> Preferences window of the Arduino IDE).
If you don't know where the “libraries” folder is, you can add the library
(LiquidCrystal_I2C) by opening the Arduino IDE, choose Sketch -> Include Libray
-> Add ZIP Library and link to the .zip file you downloaded earlier.

• To install the DHT library, select Sketch > Include Library > Manage
Libraries:
In the opened window, search “DHT11” and install “DHT sensor library”
Version 1.4.4.

Before displaying text on the LCD, we need to find the LCD I2C address. With
the LCD properly wired to the ESP8266, upload the following I2C Scanner sketch.
#include <Wire.h>

void setup() {
Wire.begin();
Serial.begin(115200);
Serial.println("\nI2C Scanner");
}

void loop() {
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ ) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
nDevices++;
}
else if (error==4) {
Serial.print("Unknow error at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found\n");
}
else {
Serial.println("done\n");
}
delay(5000);
}

After uploading the code, open the Serial Monitor at a baud rate of 115200.
The I2C address should be displayed in the Serial Monitor.

In this case the address is 0x27. If we’re not using a similar 16×2 display, we’ll
probably get the different address.
Write below codes to your Arduino IDE, and choose the corresponding board
type and port type for project, upload the sketch to the NodeMCU.
3.2.1. Read data from DHT 11 sensor:
Code:
#include "DHT.h" // declaration library DHT11

const int DHTPIN = 14; //read data from DHT11 at GPIO14 pin
connected to sensor (D5)
const int DHTTYPE = DHT11; //declaration the sensor type, there are 2
types: DHT11 and DHT22

DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
dht.begin(); // sensor start
}

void loop() {
// wait a few seconds between measurements
delay(10000);
float h = dht.readHumidity(); //Read humidity
float t = dht.readTemperature(); //Read temperature in degrees Celsius
float f = dht.readTemperature(true); //Read temperature in degrees
Fahrenheit

Serial.print("Do am: ");


Serial.print(h); // display humidity
Serial.print("%\t");
Serial.print("Nhiet do: ");
Serial.print(t); // display temperature C
Serial.print(" *C ");
Serial.print(f); // display temperature F
Serial.println(" *F ");

Serial.println();
}
After uploading the code, open the Serial Monitor at a baud rate of 9600. The
temperature and humidity should be displayed in the Serial Monitor.
3.2.2. Display static text on the LCD
Code:
// Display static text on the LCD

#include <LiquidCrystal_I2C.h>

// set the LCD number of columns and rows


int lcdColumns = 16;
int lcdRows = 2;

// set LCD address, number of columns and rows


LiquidCrystal_I2C lcd(0x00, lcdColumns, lcdRows);

void setup(){
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
}

void loop(){
// set cursor to first column, first row
lcd.setCursor(0, 0);
// print message
lcd.print("Hello, World!");
delay(1000);
// clears the display to print new message
lcd.clear();
// set cursor to first column, second row
lcd.setCursor(0,1);
lcd.print("I love Phenikaa!");
delay(1000);
lcd.clear();
}

Note: Please correct the code by setting the LCD address accordingly!
3.2.3. Read temperature and humidity from DHT11 and show to LCD display
Now you know how to read temperature and humidity from DHT11 sensor and
also know how to display static text on the LCD. So now we need to combine the
two to get a code like this:

// Read temperature and humidity from DHT11 and show to LCD display

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

LiquidCrystal_I2C lcd(0x00,16,2);

const int DHTPIN = 14;


const int DHTTYPE = DHT11;
DHT dht(DHTPIN, DHTTYPE);

byte degree[8] = {
0B01110,
0B01010,
0B01110,
0B00000,
0B00000,
0B00000,
0B00000,
0B00000
};

void setup() {
lcd.init();
lcd.backlight();

lcd.print("Nhiet do: ");


lcd.setCursor(0,1);
lcd.print("Do am: ");

lcd.createChar(1, degree);

dht.begin();
}

void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(t) || isnan(h)) { // Check if the read value failed or not.
http://arduino.vn/reference/isnan
}
else {
lcd.setCursor(10,0);
lcd.print(round(t));
lcd.print(" ");
lcd.write(1);
lcd.print("C");

lcd.setCursor(10,1);
lcd.print(round(h));
lcd.print(" %");
}
}

Note: Please correct the code by setting the LCD address accordingly!

4. Homework:
a) Learn about different types of sensors and actuators
b) Study the meaning of command lines in sketches, how the code Works?
c) Write a practice report

You might also like