Lesson 5 UltraSonic Distance Sensor - levizzzle/CS490 GitHub Wiki

Lesson 5: Revolved around using the HC-SR04 distance sensor and creating a condition to set off an alarm state when getting too close to the sensor. I set my threshold at 7cm and upon going below that distance, would raise an alarm. The alarm state was a single second interval in which a red LED would light up and a buzzer would sound. If further than 7cm away, the alarm state will not activate. This was followed up by connecting to the Raspberry Pi and running a Python script to pull sensor data from the Arduino using serial and then looping over a 10 second period while writing each value into a "distances.txt" file.

Components used:

  • Arduino Uno R3
  • Breadboard
  • HC-SR04
  • Raspberry Pi 3
  • 5mm Red LED
  • 470 Ohm Resistor
  • HSDZ Buzzer
  • QAPASS 1602 LCD Monitor

Screenshot of Arduino and connected components

Screenshot of Alarm State

Arduino Code------------------------------------------------

//Libraries
#include <LiquidCrystal_I2C.h>
#include <HCSR04.h>

//Constants
int triggerPin = 12; //Set trigger pin
int echoPin = 11; //Set echo pin
int buzzerPin = 13; //Set buzzer pin
int ledPin = 10; //Set LED pin
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //LCD screen configuration
UltraSonicDistanceSensor distanceSensor(triggerPin, echoPin); //Initialize UltraSonic Distance Senor with

//Variables
double distance;

void setup()
{
  //Set pins for ouput
  pinMode(buzzerPin,OUTPUT);
  pinMode(ledPin,OUTPUT);
  
  //Initialize Serial, LCD monitor, clear screen
  Serial.begin(9600);
  lcd.begin(16,2); 
  lcd.clear(); 
}

void loop()
{
    distance = distanceSensor.measureDistanceCm(); //Set distance variable to sensor reading
    Serial.println(distance); //Print distance to serial
    
    if(distance < 7){ //If distance is less than 7 centimeters
        lcd.clear(); //Clear screen
        lcd.print("TOO CLOSE: "); //Print "TOO CLOSE: " to LCD
        lcd.print(distance); //Print distance to LCD
        digitalWrite(buzzerPin, HIGH); //turn on buzzer
        digitalWrite(ledPin, HIGH); //turn on LED
        delay(1000); //Wait for 1 second
        digitalWrite(buzzerPin, LOW); //turn off buzzer
        digitalWrite(ledPin, LOW); //turn off LED
        delay(1000); //Wait for 1 second
    }
    else{
        lcd.clear(); //Clear screen
        lcd.print("Value: "); //Print "Value: " to LCD
        lcd.print(distance); //Print distance to LCD
        delay(1000); //Wait for 1 scond
    }
}

Python Code------------------------------------------------

#Libraries
import serial
import time
import sys

#Create distance file
distFile = open("distances.txt","w+")

#Assignment Variables
ser = serial.Serial("/dev/ttyACM1",9600) #Connect to serial
ser.baudrate = 9600 #Set baudrate

#Read from serial
for x in range(1,11): 
    read_ser = ser.readline() #Read line
    dist = read_ser.decode('ASCII') #Decode ASCII characters
    distFile.write("Value "+str(x)+": "+str(dist)) #Write line to file
    time.sleep(1) #Sleep 1 second
    
#Close file    
distFile.close()
print ('"distance.txt" created with 10 seconds of distance readings')

Link to Youtube Video

Things Learned

Lesson 5 taught me how to use the HC-SR04 Distance Sensor and how conditions can be applied as part of the Arduino loop to create an alarm state. This presented the ability to have a different state on the LCD screen in as well as turning on the LED and buzzer. Additionally I learned how to gather multiple pieces of data from serial using a loop combined with the time module. I also learned how to write to a text file using python.

⚠️ **GitHub.com Fallback** ⚠️