Tuesday, July 12, 2022

Arduino Uno and HC-SR04 Distance Measurement

 An ultra-sonic distance measurement module is very popular for electronic hobbyists, especially in vehicle robot. The HC-SR04 module is widely available at very low cost. It use the ultra-sonic sound transmitting/receiving in the air to get the distance of any detected object. It communication interface is very simple.

Arduino Uno and Distance Measurement
Program running on breadboard

In this example, I make a typical distance measurement using the HC-SR04 module with Arduino Uno. I added an I2C to LCD converter module to get a easily circuit wirings. The distance will show in centimeters. The HC-SR04 has only four pins:

  1. VCC
  2. Trigger
  3. Echo
  4. and GND
Arduino Uno and Distance Measurement
HC-SR04 an from Ali-Express store

Trigger pin is the input pin that keep listening to the controller for triggering the distance finding. Echo pin is the output pin that output a high logic signal to the controller. Its output duration in micro-seconds relate to the detected distance in centimeters. We can see its specification below.

Arduino Uno and Distance Measurement
Electric Parameter

There are only two signal pins - trigger , and echo pin.

Arduino Uno and Distance Measurement
Timing diagram

The Arduino can calculate the distance in centimeter as follow.

distance = duration (in microseconds)/58

or distance = duration * 340 /2

It schematic consists of a little wiring lines.

Arduino Uno and Distance Measurement
Wiring diagram

Source Code:

  1. //Two-Wire
  2. #include <Wire.h>
  3. #include <LiquidCrystal_I2C.h>
  4.  
  5. //LCD Setting, PCF8574T address is 0x27, 16x2
  6. LiquidCrystal_I2C lcd(0x27,16,2);
  7.  
  8. #define triggerPin A0
  9. #define echoPin A1
  10.  
  11. void setup(){
  12. //Initialize the LCD
  13. lcd.init();
  14. //Turn on the back light
  15. lcd.backlight();
  16. //Print some information
  17. lcd.print("Arduino Distance");
  18. lcd.setCursor(0,1);
  19. lcd.print(" Measurement ");
  20. delay(3000);
  21. lcd.clear();
  22.  
  23. lcd.home();
  24. lcd.print("Distance:");
  25.  
  26. //HC-SR04 Pins Setting
  27. pinMode(triggerPin,OUTPUT);
  28. pinMode(echoPin,INPUT);
  29. }
  30.  
  31. void loop(){
  32. //Activate the distance sensor
  33. digitalWrite(triggerPin,HIGH);
  34. delayMicroseconds(10);
  35. digitalWrite(triggerPin,LOW);
  36.  
  37. //Get the distance, time out is 60ms
  38. unsigned int distanceMicro = pulseIn(echoPin,HIGH,60000);
  39.  
  40. //Calculate the distance in cm
  41. distanceMicro/=58;
  42.  
  43. lcd.setCursor(0,1);
  44. if(distanceMicro>400) lcd.print("Invalid ");
  45. else lcd.print(String(distanceMicro,DEC)+"cm ");
  46.  
  47. delay(1500);
  48. }
  49.  

Click here to download this example in zip file format.

Sunday, July 3, 2022

Arduino Uno Temperature and Humidity Reading with DHT-11 Sensor

DHT-11 is a temperature and humidity sensor module. It use a serial data transmission between the module and the controller. It has the following features:

  • Operating Voltage: 3.5V to 5.5V
  • Operating current: 0.3mA (measuring) 60uA (standby)
  • Output: Serial data
  • Temperature Range: 0°C to 50°C
  • Humidity Range: 20% to 90%
  • Resolution: Temperature and Humidity both are 16-bit
  • Accuracy: ±1°C and ±1%

Arduino Uno Temperature and Humidity Reading with DHT-11 Sensor
A simple DHT-11 Sensor
It's suitable for in-house temperature controlling, or the irrigation system. This module is very popular to apply with the Arduino. The DATA pin of this module is the serial data I/O pin. It requires an additional pull up resistor, typically a 5kOhm resistor.

Arduino Uno Temperature and Humidity Reading with DHT-11 Sensor
Typical connection
In this example, the Arduino Uno will read the environmental data from this module, and it will show on a character display. The LCD I use here is a 16x2 character LCD. I use an additional PCF8574T I2C to parallel I/O to the LCD.

Arduino Uno Temperature and Humidity Reading with DHT-11 Sensor
Arduino running program

The pull up resistor I used here is 10kOhm.

  1. //Two-Wire library
  2. #include <Wire.h>
  3. #include <LiquidCrystal_I2C.h>
  4. //DHT-11 Sensor Library
  5. #include <SimpleDHT.h>
  6.  
  7. //Default address 0x27, LCD is 16x4
  8. LiquidCrystal_I2C lcd(0x27,16,4);
  9.  
  10. //DHT-11 connects to pin A0
  11. SimpleDHT11 dht11(A0);
  12.  
  13. void setup(){
  14. //Initialize the LCD
  15. lcd.init();
  16. //Turn on the backlight
  17. lcd.backlight();
  18.  
  19. //show some information
  20. lcd.print("BTE Technician");
  21. lcd.setCursor(0,1);
  22. lcd.print("Arduino Uno And");
  23. lcd.setCursor(-4,2);
  24. lcd.print("DHT-11 Sensor");
  25. lcd.setCursor(-4,3);
  26. lcd.print("LCD Example");
  27. delay(3000);
  28. lcd.clear();
  29. }
  30.  
  31. byte temperature,humidity;
  32.  
  33. void loop(){
  34. //Get temperature, humidity, and no raw data
  35. dht11.read(&temperature,&humidity,NULL);
  36.  
  37. lcd.setCursor(0,0);
  38. lcd.print("Temperature:");
  39.  
  40. lcd.setCursor(3,1);
  41. lcd.print(String(temperature,DEC)+" "+char(223)+"C");
  42.  
  43. lcd.setCursor(-4,2);
  44. lcd.print("Humidity:");
  45.  
  46. lcd.setCursor(-1,3);
  47. lcd.print(String(humidity,DEC)+" %RH");
  48. delay(3000);
  49. }

Its wiring diagram is shown below.

Arduino Uno Temperature and Humidity Reading with DHT-11 Sensor
Wiring diagram

Click here to download its source file.

Saturday, July 2, 2022

PCF8574T I2C LCD Example with Arduino Uno

In previous post, I showed about the character LCD interfacing with Arduino Uno. It use a parallel data transmission method between the LCD and the Arduino module. We can add a serial to parallel converter IC to make an ease of connection between those two modules. The PCF8574T is an I2C to 8-bit parallel I/O converter chip, comes with small size. It's suitable for character LCD controlling. I2C uses only two wire, SDA, and SCL.

PCF8574T I2C LCD Example with Arduino Uno
PCF8574T character LCD interface module

There are many LCD controlling module based on the PCF8574T on online stores. It's ready to use, since they are designed for character LCD. Some of them are soldered and sold with the character LCD module at low price.

In this example, I use this module connecting with a 16x2 character LCD. It will show a total duration since the Arduino started up.

PCF8574T I2C LCD Example with Arduino Uno
Running program

I use two libraries in this example,

  1. Wire.h, 
  2. TimerOne.h
  3. and LiquidCrysal_I2C.h 

The Wire.h comes with the IDE. But the other two's we need to install. Two I2C pins stay with the same pin of the Arduino analog inputs,

  1. SDA - A4
  2. SCL  - A5

The default I2C address of PCF8574T 0x27, whenever its three address pins are opened.

PCF8574T I2C LCD Example with Arduino Uno
Address pin for PCF8574T module

However it's not necessary to change its address here.

  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3.  
  4. #include <TimerOne.h>
  5.  
  6. //Default address 0x27, LCD is 16x2
  7. LiquidCrystal_I2C lcd(0x27,16,2);
  8.  
  9. void setup(){
  10. //Initialize the LCD
  11. lcd.init();
  12. //Turn On Backlight
  13. lcd.backlight();
  14.  
  15. lcd.print("PCF8574T I2C LCD");
  16. lcd.setCursor(0,1);
  17. lcd.print("Arduino Example");
  18. delay(3000);
  19. lcd.clear();
  20.  
  21. lcd.home();
  22. lcd.print("Powered Up Time:");
  23.  
  24. //Timer1 duration is 1 second
  25. Timer1.initialize(1000000);
  26. Timer1.attachInterrupt(secondCounts);
  27. }
  28.  
  29. int days,hours,minutes,seconds;
  30.  
  31. void loop(){
  32. lcd.setCursor(0,1);
  33. //Show the days, hour, minute, and second
  34. lcd.print(String(days)+" "+String(hours)+":"+String(minutes)+":"+String(seconds)+" ");
  35. }
  36.  
  37. void secondCounts(){
  38. seconds++;
  39.  
  40. //Finding the minutes
  41. if(seconds>=60){
  42. minutes++;
  43. seconds=0;
  44. }
  45. //Finding the hours
  46. if(minutes>=60){
  47. hours++;
  48. minutes=0;
  49. }
  50. //Finding the days
  51. if(hours>=24){
  52. days++;
  53. hours=0;
  54. }
  55. }

It schematic diagram is very simple.

PCF8574T I2C LCD Example with Arduino Uno
Wiring diagram

Click here to download its source file.


Labels

ADC (10) Analog (14) Arduino (12) Atmega16 (19) Audio (2) AVR (20) Charger (1) Cortex-M0 (1) Counter (10) CPLD (25) Digital I/O (22) Display (34) EEPROM (2) Environment Sensor (1) esp8266 (2) Experiment Board (10) I2C (4) Interrupt (7) LCD (1) LDmicro (29) measurement and instrumentation (7) Microchip Studio (3) MikroC (1) One-Shot (3) OpAmp (1) PCB (31) PIC16 Microcontrollers (16) PIC16F877A (2) PIC16F887 MikroC (22) PLC (35) PWM (11) Regulator (1) RTC (2) Sensor (8) Shift Registers (5) SPI (5) Timer (34) UART (2) ultra-sonic sensor (1) USB (1) VHDL (21) xc8 (1) XC95108 (9) XC9536 (15) XC9572 (1) Xilinx (23) Xilinx ISE (22)