Parking Sensor using Arduino

Parking Sensor using Arduino

What is an Arduino?


4.jpg

Arduino is a single-board microcontroller, intended to make it easier to build interactive objects or environments. The hardware consists of an open-source hardware board designed around an 8-bit Atmel AVR microcontroller, or a 32-bit Atmel ARM. It CPU, flash, EEPROM and static RAM memory and timers. Current models feature a USB interface, 6 analog input pins, as well as 14 digital I/O pins that accommodate various extension boards.

Our project


The project that we implemented is a parking sensor system using an Arduino board, an HC-SR04 ultrasonic distance sensor, LEDs, buzzer and assorted wires. As the car gets closer to an obstruction, the LEDs blink more rapidly and the buzzer sound duration decreases ie. frequency of beeps increases.

The sensor


For this project we used an ultrasonic distance sensor. As the name suggests, the sensor measures distance by emitting ultrasonic sound waves and measuring the time of between emission and return of wave. The HC-SR04 sensor module includes ultrasonic transmitters, receiver and control circuit.
Below on the left is a front view of the sensor, and on the right a view from the back.

2.jpg
3.jpg

Working of the sensor


The HC-SR04 module has 4 pins:

  • Vcc
  • Trig
  • Echo
  • Gnd

When the HC-SR04 receives a short pulse on the trigger input the module begins a measurement. First it transmits a short ultrasonic pulse then it listens for the echo. The trigger pulse needs to be at least 10 µS long and will be delivered from pin 2 on the Arduino Uno. Thereafter the sensor emits an 8 cycle burst of 40kHz pulses.
When the ultrasonic burst of pulses has been trasmitted by the HC-SR04 the signal voltage on the Echo pin goes high. It will remain high for up to 25 mS depending on the distance from the object. If there is no object in view then the Echo signal will stay high for 38 mS. So all we have to do is measure the time during which the Echo signal is high. From that, together with the speed of sound, we can calculate the distance to the object.

Formula for calculation of distance


(1)
\begin{equation} D = (T * S) / 2 \end{equation}

Where
D = Distance
T = High level time
S = Speed of sound

Hardware Setup


The hardware setup is done as per the following circuit diagram :

5.jpg

The completed circuit :

1.jpg

Software Setup


The Arduino comes with its own IDE. Programs for the Arduino are based on the C programming language are referred to as ‘sketches’, as the first users were artists and musicians.
The program consists of two basic functions: setup() and loop(). The setup function runs just once and is used to initialize all the hardware components. After that the loop() function runs continuously.

Code


#include <TimerOne.h>                                 
 
#define trigPin 4                                     
#define echoPin 2                                     
#define onLED 12                                      
#define echo_int 0                                    
 
#define TIMER_US 50                                    
#define TICK_COUNTS 4000                              
 
volatile long echo_start = 0;                          
volatile long echo_end = 0;                           
volatile long echo_duration = 0;                      
volatile int trigger_time_count = 0;                  
volatile long range_flasher_counter = 0;              
 
void setup() 
{
  pinMode(trigPin, OUTPUT);                           
  pinMode(echoPin, INPUT);                          
  pinMode(onLED, OUTPUT);                        
 
  Timer1.initialize(TIMER_US);                        
  Timer1.attachInterrupt( timerIsr );                  
  attachInterrupt(echo_int, echo_interrupt, CHANGE);  
  Serial.begin (9600);                                
}
 
void loop()
{
    Serial.println(echo_duration / 58);              
    delay(100);                                       
}
 
void timerIsr()
{
    trigger_pulse();                                 
    distance_flasher();                              
}
 
void trigger_pulse()
{
      static volatile int state = 0;                 
 
      if (!(--trigger_time_count))                   
      {                                              
         trigger_time_count = TICK_COUNTS;           
         state = 1;                                  
      }
 
      switch(state)                                  
      {
        case 0:                                      
            break;
 
        case 1:                                      
           digitalWrite(trigPin, HIGH);              
           state = 2;                                
           break;
 
        case 2:                                      
        default:      
           digitalWrite(trigPin, LOW);               
           state = 0;                                
           break;
     }
}
 
void echo_interrupt()
{
  switch (digitalRead(echoPin))                     
  {
    case HIGH:                                      
      echo_end = 0;                                 
      echo_start = micros();                        
      break;
 
    case LOW:                                       
      echo_end = micros();                          
      echo_duration = echo_end - echo_start;        
      break;
  }
}
 
void distance_flasher()
{
      if (--range_flasher_counter <= 0)                
      {                                                
         if (echo_duration < 25000)                    
         {
           range_flasher_counter = echo_duration * 2;  
         }
         else
         {
           range_flasher_counter = 25000;              
         }
 
         digitalWrite( onLED, digitalRead( onLED ) ^ 1 );   
      }
}

Explanation of code


When the program is loaded the first thing it does is execute the setup() function. The various pins of the Arduino are set to input or output as appropriate. Timer 1 is initialized, the interrupt service routines attached to the appropriate interrupts and the serial port is initialized.
The background loop() function then begins to run. Any routines placed in loop() are executed continuously. All of the code required to handle the HC-SR04 and the onboard LED is contained in interrupts routines so the only task in the loop() function is to send the distance measured in centimeters to the serial monitor port every 100 mS.
timerIsr() is called every time the timer 1 times out and creates an interrupt. To simplify the code this function calls two more functions. One to handle the trigger pulse and the other one to flash the onboard LED.
The trigger_pulse() function is called from timerIsr() every 50 uS and creates a trigger pulse every 4000 calls or 200 mS. The pulse generator is implemented with a simple finite state machine contained in the switch() statement. Every time this routine is called exactly one of the cases is executed. When the count reaches 200 mS the state is changed from 0 to 1 which immediately causes the trigger output to be set high and the state changed to 2. When the function is next called 50 mS later, case 2 is called which resets the trigger output to low and the state reset to 0.
echo_interrupt() is called every time the echo input signal changes state and generates an interrupt. The echo input is tested to determine whether this is the start or end of the echo pulse and start and end times recorded accordingly. If the input has changed from high to low then this is the end of the pulse and the duration is computed.
The distance_flasher() function is called from timerIsr() every 50 uS. A counter is decremented and if it reaches zero then the onboard LED is toggled and the counter is reloaded with the value of the computed echo pulse duration. This has the effect of flashing the LED faster the closer the object is to the sensor.

Video Demonstration


Bibliography


1. Morgolis, Michael. Arduino Cookbook, 2nd Edition (O'Reilly Media, 2011), ISBN-13: 978-1449313876, ISBN-10: 1449313876
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License