Categories
TUTORIALS

How to make an Arduino toll-gate with Ultrasonic Sensor, Servo, and LCD Screen

Introduction

In this tutorial, we will learn how to use the 1) Ultrasonic Sensor 2) Servo and 3) LCD Screen with the Arduino Board to prototype a toll-gate for vehicles. This system is part of a larger train system I am working on with my group mates Leo and Neil, who each has also written wonderful tutorials on LED strip pattern response and peak weight detection using Arduino on this site – do check them out!

This system uses the Ultrasonic Sensor to detect nearby objects, and translates this information into motor motion in the Servo (the ‘toll gate’). Assuming the role of the serial monitor in Arduino, the LCD screen displays the distance between the Ultrasonic Sensor and the nearby object, and describes the action the ‘toll gate’ is about to take. The figure below illustrates this process.

 

You can also watch the following videos to better understand how this system performed!

The Toll-Gate response

The LCD Screen Response

 

The Components and Set-up

The components required for this tutorial’s set-up are listed below. Some components might not be available in your starter Arduino kit – highlighted are suggested links to sites where you can purchase those parts.

  • Ultrasonic Sensor……………………………………………………Amazon
  • Micro Servo Motor…………………………………………………Amazon
  • LCD Screen………………………………………………………………Amazon
  • Potentiometer…………………………………………………………from Arduino kit
  • Arduino Board…………………………………………………………from Arduino kit
  • Breadboard and Jumper Wires……………………………from Arduino kit

Because I love trains more than cars, my set-up closely mimics a train station system, with the ultrasonic sensor placed beneath the track to detect stationary trains at train stations:

(Feel free to come up with your own set-up design!)

(This was also a purely unintentional product placement. Really!)

A Note on How the Ultrasonic Sensor worked in this tutorial

The Ultrasonic Sensor comprises an emitter and a receiver. The emitter emits sounds ultrasound waves at 40 000 Hz that travels away from the sensor and through the air. If the sound waves hits an obstacle, they are reflected in an opposite direction back to the sensor, and received by the receiver. The time in microseconds between the emission and reception is recorded by the sensor.

This allows us to calculate the distance between the sensor and the object:

The sensitivity of the Ultrasonic Sensor may differ for different ranges of distances. When I was tinkering with the sensor, I realised that its accuracy in detecting very near objects (less than 2cm away) is highly inconsistent. My guess is that the Ultrasound is reflected too quickly back to the sensor, which then fails to register it as the reflected product of what was just emitted. Therefore, I set the minimum distance away from the sensor as 4cm, in order to produce a consistent response.

Wiring

As you have probably observed from the video, wiring for this project can get messy. Hopefully the Breadboard Diagram here helps:

Not pictured above is the power source – connecting this to your laptop or an external battery source works. Wires connecting to/from the 5V power source are red, while those connecting to/from the Ground are black.

As someone easily confused by too many bits and pieces, I found that wiring this section by section helps with managing the bundle of crisscrossing wires involved:

1. The Ultrasonic Sensor

The Ultrasonic Sensor I used (HC-SR04) has 4 pins in this order: Ground, Echo, Trig, and VCC.

Connect the Ground and VCC to the Ground and the 5V pins on the Arduino Board respectively. You can then connect the Echo and Trig pins to any of the Digital I/O pin on the Arduino Board. In this tutorial, I connected the Echo and Trig to 9 and 10 respectively.

2. The LCD Screen and its mate the Potentiometer

Wiring this part made me cross-eyed. Hopefully the figure below helps you  avoid my pain:

As you can see, there are 16 pins on the LCD module.  The last pair of pins (Anode and Cathode) completes the circuit that powers the backlight of the screen. This gives the LCD screen its constant blue background. Characters on the screen are displayed via contrasting white light. The first pair of pins completes the circuit that powers the contrast letters on the screen ,  and you should connect the third pin V0 to the middle pin of the potentiometer to adjust the contrast of these characters against the blue light. (I didn’t do this in the beginning and thought that I bought a dud when the letters failed to show up. Then I realised – I was the dud.)

The RS pin (Register Select) is used for selecting whether we will send commands or data to the LCD. When the RS pin is set on low state or zero volts, we are sending commands to the LCD . Such commands include setting the cursor to a specific location, clearing the display, turning off the display and so on. When RS pin is set on high state or 5 volts, we are sending data or characters to the LCD. These characters are what we ultimately read off the LCD screen. Here, I connected RS to Pin 1.

We use the RW (Read Write) pin to select whether we are reading from or writing to the LCD screen. This pin is connected to ground. Lastly, the E pin enables the writing to the registers – the next 8 data pins from D0 to D7. Here, I wired E, D4, D5, D6, D7 to Pin 2, 4, 5, 6, 7 respectively.

You may have noticed that I have only used the last 4 data pins from D4 to D7. This is because I am using the LCD in 4-bit mode. The Liquid Crystal library also allows the use of the LCD in 8-bit mode – this will fully utilise all 8 pins from D0 to D7 then.

If you are still interested to find out more on how this module work, Dejan on HowToMechatronics provided a really thorough description of what each pin on the LCD does, and I understood everything I described above from his site.

Connect the first and last pin of the potentiometer to Ground and 5V – the order does not matter!

3. The Servo Motor

The Servo Motor has 3 pins. Connect the yellow one to a pin on the Arduino board; in this tutorial my pin of choice was 13. Connect the red pin to 5V, and black pin to Ground.

Code

Coding is the simple part of this tutorial! First set up according to how you wired the pins – if you decide to use different pin combinations for your own purposes, do remember to change the numbers accordingly when defining the variables.

The purpose of each code line is also included below.

I hope this tutorial was useful – let me know if you have any questions or ideas on how I can improve this!

4 replies on “How to make an Arduino toll-gate with Ultrasonic Sensor, Servo, and LCD Screen”

code is showing some error

Arduino: 1.8.16 (Windows 10), Board: “Arduino Uno”

C:\Users\Yash\OneDrive\Documents\Arduino\sketch_sep24c\sketch_sep24c.ino: In function ‘void loop()’:

sketch_sep24c:73:3: error: expected ‘}’ at end of input

}

^

sketch_sep24c:73:3: error: expected ‘}’ at end of input

exit status 1

expected ‘}’ at end of input

This report would have more information with
“Show verbose output during compilation”
option enabled in File -> Preferences.

C:\Users\Yash\OneDrive\Documents\Arduino\sketch_sep24c\sketch_sep24c.ino: In function ‘void loop()’:

sketch_sep24c:73:3: error: expected ‘}’ at end of input

}

^

sketch_sep24c:73:3: error: expected ‘}’ at end of input

exit status 1

expected ‘}’ at end of input

This report would have more information with
“Show verbose output during compilation”
option enabled in File -> Preferences.

Leave a Reply

Your email address will not be published. Required fields are marked *