I found a project that calculate the distance using ultrasonic, which I liked but of course I will put my touch on it, So lets see how.
Software and Material used:
For circuits simulation I used Tinkercad (online website), to implement the connections.
Design and Preparations:
When it comes to electronic parts, it is recommended to look at the datasheet of each components to know the operating voltage and current ranges, polarity and if it is connected with more than 2 terminals, and this is very important to avoid damage that can affect all the circuits components.
This circuit consist of a Ultrasonic sensor, LCD , key pad and a led so lets get more information about them.
Ultrasonic:
This sensor made to calculate the nearest object distance, it can measure up to 16m distance, the tow cylindrical parts are transducers one send an ultrasonic wave and the other receive it and by calculating the duration between sending the signal and receiving it with default sound speed (around 340m\s) it is easy to calculate the distance.
The 4 terminals of the ultrasonic are:
VCC: connected to 5volt
Trig: connecter as output pin to the Arduino
Echo: connected as input pin to the Arduino
GND: connected to the ground.
LCD(I2C):
The LCD with I2C consists of 4 terminals 2 for the power and ground and 2 for data (SDA,SCL).
i used a library to operate the LCD called "LiquidCrystal_I2C". which is designed to connect SDA,SCL to A4,A5 respectively.
Keypad:
4*3 keypad is a 7terminals components, 4 terminals for the 4 rows and 3 terminals for the 3 columns.
To simulate this project with Tinkercad open the website, then create a new circuit design, and by drag and drop you can easily bring all of the components to your design.
after dragging the Arduino, Ultrasonic sensor, LCD, keypad and LED now its time to set the connections.
We can go to implement the whole circuit on the breadboard, which is a board that is make circuits testing very easy, by fitting male terminals into it's female bins which are connected together as shown.
Adruino Code File : Door in/out Counter
LCD connection and code:
the 4 bins in the I2C make it more easy to connect the LCD screen, SDA,SCL to A4,A5 respectively, Vcc to 5volts bin and ground to ground.
A variable called (State) is created to avoid multiple count with the by looking at the code first thing we need to include the LCD library "#include <LiquidCrystal_I2C.h>" the define that this library has 2 rows and 12 column "LiquidCrystal_I2C lcd(0x27, 16, 2);" and setting back light in setup() function.
by this LCD is ready to use "lcd.clear()" & "lcd.print()"
KeyPad:
similarly the keypad require to connect the 7 bins and include the "#include <Keypad.h>" library then determine the columns bins and rows bins as following:
By implementing the circuit exactly the same as the simulation, it will get a little bit crowded weirs as we notice about 20 jumpers is used, so it is hard to clarify the connections using real photos.
now it is ready to read the characters using "char key = keypad.getKey();"
as this function returns a "char" and I will need to use it as "integer" to make operations on it, So wrote this 'nested if' to transfer "char" to "int".
UltraSonic:
not like the pervious one Ultrasonic has a written function that calculate the duration between sending a signal and receiving the echo.
after implementing all of these parts together and combining all these code together and setting up the conditions of "save" and "delete" with "*" and "#" buttons the final code can be found here:
This project is designed to calculate the distance and compare it to the one that entered by user by keypad.
the green led is turned on once the measured distance is close to the desired and with 5cm tolerance, otherwise the red led is turned on.
saving the integer "char" to a 3digit number was a challenge, but with trial and error I could solve it by adding an array to save it.