How do I start?
The first thing to do is to take a look at our photos above to see how the circuit is constructed, and then have a go building it yourself! Hint: Use the circuit diagram when building it, it’s there for a reason.
I know how to do wire it just shut up and show me the code.
The system consists of two main parts, the transmitter side and the receiver side. The transmitter side includes:
1) Two JSN-SR0T4-2.0 ultrasonic distance measurement modules, these have a reliable range of 30-600cm in my experience. 2) 1 NRF24L01 radio comms transceiver, a transceiver can act as both a transmitter and a receiver. 3) A Push to Make button. 4) Breadboard. 5) Arduino. 6) DHT11 Temperature and Humidity Sensor. 7) A 9V battery. 8) Far too many jumper wires
One the receiver side of the Parking Sensor System things are a little less complex. It includes:
1) 16×2 LCD screen. 2) 1 NRF24L01 radio comms transceiver, a transceiver can act as both a transmitter and a receiver. 3) A Potentiometer (also known as a variable resistor). 4) Breadboard. 5) Arduino. 6) Also far too many jumper wires.
Normally at this stage I would give you a brief break down as to how the system is wired but due to the amount of wiring in this Parking Sensor project, I feel like it may cause more confusion than anything. I will however focus on a few bits and pieces that may need further explanation.
The first thing I want to talk about is the JSN-SR04T-2.0 Ultrasonic sensors. Interestingly enough these can be swapped with a HC-SR04 Ultrasonic Sensor as a drop in replacement as the wiring is the same. These may be easier to get your hands on than the JSN-SR04T-2.0. Additionally the JSN-SR04T (without the 2.0) will also work, and I have heard even better reviews about it than the JSN-SR04T-2.0.
Here you need to be careful not to mix up the Trig and Echo pins on the JSN-SR04T-2.0 while wiring, as this will give you problems. In the next section we will explain how it calculates the distances.
The next important thing to note is when wiring the DHT11, it is very important to remember the 10K Ohm resistor between pin 1 and 2. Also be sure to use the libraries provided as I’ve heard of people having issues with some of the DHT libraries online, but I know this one has worked for me.
The buzzer is not necessary to the operation of the rest of the system, if you do not want it to buzz you can simply remove the buzzer. Do not forget to include the 220 Ohm Resistor while wiring.
The button can be used to simulate the car being in reverse, thus changing the data on the LCD from Temperature and Humidity to the Distance detected by the Ultrasonic Sensors. However, if you want to for testing you can short out the button to show the distance at all times. That is what I’ve done in the video.
The LCD screen is the 16pin version, there is 4pin variant, which may require a different library. But if you have that type of LCD screen you should be able to use it without much more work.
The last thing I have to mention is that the potentiometer can be replaced with a normal resistor if you can find a resistor that gives you the correct brightness. However I find that variable resistor makes it easy to see the screen in different lighting conditions.
WARNING: When uploading code make sure you remove the wires going into the RX and TX pins on the Arduino, these are pins 0 and 1, as these are also used by the USB and will cause your code to fail uploading.
How do we use the JSN-SR04T-2.0 to calculate the distance?
Below you can see the code snippet for one ultrasonic sensor sending out a ultrasonic sound wave and then receiving the time it took to return.
digitalWrite(trigPin, LOW); //set trigger pin to LOW
delayMicroseconds(5); //delay of 2 microseconds
digitalWrite(trigPin, HIGH); //set trigger pin to HIGH
delayMicroseconds(20); //delay of 20 microseconds
digitalWrite(trigPin, LOW); //set trigger pin to LOW
timeTaken = pulseIn(echoPin, HIGH);
The following code, sets a LOW voltage to the trigger pin of the module, ensuring that it is NOT emitting any sound. We set it to LOW first in case there is any idle voltage on the pin. If you do not set the pin to either HIGH or LOW you can get what is called a “floating voltage” and it can vary from anywhere from 0V to VCC (5V in this case), which may give you unusual activity.
Okay so we set the Trig Pin to LOW so it’s not active. And delay for 5 microseconds, just long enough to ensure the pin is definitely LOW.
digitalWrite(trigPin, LOW); //set trigger pin to LOW
delayMicroseconds(5);
Next we set the Trig Pin to high and delay for 20 micro seconds which is long enough for the module to send out the sound waves.
digitalWrite(trigPin, HIGH); //set trigger pin to HIGH
delayMicroseconds(20);
Then we drive the Trig pin LOW again to make sure it is no longer emitting ultrasonic waves.
digitalWrite(trigPin, LOW); //set trigger pin to LOW
We use the pulseIn function to detect when the state of the Echo pin changes from Low to High, then times how long it takes to go back to Low again. It assigns the value to a variable called timeTaken. Here we have the time taken for the sound wave to leave the module, hit an object and bounce back to the receiver.
timeTaken = pulseIn(echoPin, HIGH);
Lastly we mutliply the timeTaken for the sound wave to hit an object and return by the speed of sound to give us the distance from the module to the object and back. However we only want the distance from the module to the object, so we simply divide it by 2.
dist0 = (timeTaken*0.0343) / 2;
Now open the Arduino IDE and copy the code below
If you don’t know how to do this, check out our Arduino Sketch set up tutorials and youtube videos.
Check out the information about different Arduino IDE specific functions given on Arduino documentation to learn more.
Below are shown two different sets of code, the first is the code used to program the receiver of the parking sensor system and the second is the code used to program the transmitter part of the system. This code is not necessarily the best or the only way to do this but it is the way I found that worked for me using my level of programming at the time.
Ensure you have included the correct libraries and have wired the circuits correctly before uploading the code as it may save you a lot of time. For this project I have heavily commented the code and will not be running through it verbally for this reason. If you have any issues please do not hesitate to contact me for help.
Here are some libraries you may need: LCD Library, NRF24L01 Library and DHT Library.
// This code shows how I programmed the receiver part for the Parking Sensor System
#include <LiquidCrystal.h> //including library for LCD (16,2)
const int rs = 9, en = 8, d4 = 5, d5 = 4, d6 = 3, d7 = 2; //LCD pinout declaration
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#include <RF24.h> //include library for NRF module
RF24 radio(7, 6); // CE, CSN
const byte address[6] = "00001";
const byte numChars = 35; //assign size of array
char receivedChars[numChars]; // an array to store the received data
void setup() {
Serial.begin(115200); //open serial port with 115200 baud rate
Serial.println("<Arduino is ready>"); //print to ensure serial is open
radio.begin(); //begin radio comms
radio.openReadingPipe(0, address); //allow comms to receive
radio.setPALevel(RF24_PA_MIN);
radio.startListening(); //allow comms to receive
lcd.begin(16,2); //begin LCD screen, defining the size of the screen
}
void loop() {
showNewData();
}
void showNewData() {
if (radio.available()) {
char text[35] = ""; //create array of 35 chars
radio.read(&text, sizeof(text)); //receive data and save into the array called "text" and expect data the size that array which is 35 chars
Serial.println(text);
//loop to print temperature
lcd.setCursor(0,0); //set LCD cursor to start of 1st line to get ready to print temperature
for (int i = 0; i < 15; i++){ //points 0 - 14 in the received data array contain data for temp
lcd.print(text[i]); //print the aforementioned data from the array to the LCD
}
lcd.setCursor(0,1); //set LCD cursor to start of 2nd line to get ready to print humidity
//loop to print humidity
for (int i = 16; i < 29; i++){ //points 16 - 29 in the received data array contain data for humidity
lcd.print(text[i]); //print the aforementioned data from the array to the LCD
}
delay(100);
}
}
/*
created 17/06/2019
by Joel Gray
graycode.ie
*/
Find below the code used to program the transmitter part of the parking sensor system. This code is quite long be warned.
// This code shows how I programmed the transmitter part for the Parking Sensor System
#include <dht.h> //including library for DHT sensor and intitalising the set up
dht DHT;
#define DHT11_PIN 5
#include <RF24.h>
RF24 radio(7, 6); // CE, CSN
const byte address[6] = "00001";
//initialise pin numbers
int buzzer = 4; // buzzer pin
int trigPin = 9; // sensor trigger pin for ultrasonic sensor/ulstrasonic transducer
int echoPin = 8; // sensor echo pin
int buttonIn = 3; // button trigger pin
int trigPin1 = 1; // sensor trigger pin for second ultrasonic sensor/ulstrasonic transducer
int echoPin1 = 0; // sensor 2 echo pin
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); //open serial port with 115200 baud rate
radio.begin(); //begin radio comms
radio.openWritingPipe(address); //allow comms to transmitt
radio.setPALevel(RF24_PA_MIN);
radio.stopListening(); //make sure module is NOT receiving
pinMode(trigPin, OUTPUT); //set trigger pin as an output
pinMode(echoPin, INPUT_PULLUP); //set echo pin as an input and use internal pullup resistor
pinMode(buzzer, OUTPUT); //set buzzer pin as an output
pinMode(trigPin1, OUTPUT); //set trigger pin as an output
pinMode(echoPin1, INPUT_PULLUP); //set echo pin as an input and use internal pullup resistor
pinMode(buttonIn, INPUT_PULLUP); //define button in as input and a pull up state
digitalWrite(buttonIn, HIGH); //set buttonIn to be high
}
void loop() {
// put your main code here, to run repeatedly:
int buttonState = 0; //set button state equal to 0
buttonState = digitalRead(buttonIn); //reassign button state from the initial 0 to whatever the digital read is, in this case HIGH
digitalWrite(trigPin,buttonState); //set triggerpin to the value of buttonState, which is either high or low
long timeTaken, timeTaken1; //define variables timeTaken and dist to be 'long' which allow it to store a large number
int dist, dist0, dist1;
int prox0 = 0; //define variable prox as an integer with value 0
int prox1 = 0;
if(digitalRead(buttonIn)==HIGH) { //create if statement to complete the following commands if the button is 'HIGH'/unpressed
//define the value read from the DHT sensor as an integer
int chk = DHT.read11(DHT11_PIN); ;
int t = DHT.temperature; //assign value for temp to variable t
int h = DHT.humidity; //assign value for humidity to variable h
String stringOne = "Temp: "; //create different strings containing info to print temp and humidity to serial
String stringTwo = "C";
String stringThree = stringOne + t + char(223) + stringTwo; //creating one string using other strings //char(223) is degree symbol on LCD display
String stringFour = " "; //print spacing for LCD display
String stringFive = "Humidity: ";
String stringSix = "%"; //print % as string
String stringSeven = stringThree + stringFour + stringFive + h + stringSix;
Serial.println(stringThree + stringFour + stringFive + h + stringSix); //print different strings to serial
char charBuf[35];
stringSeven.toCharArray(charBuf, 35);
radio.write(&charBuf, sizeof(charBuf));
delay(1500);
}
if(digitalRead(buttonIn)==LOW){ //create if statement to complete the following commands if the button is 'LOW'/pressed
digitalWrite(trigPin, LOW); //set trigger pin to LOW
digitalWrite(trigPin1, LOW); //set trigger pin to LOW
delayMicroseconds(5); //delay of 5 microseconds
digitalWrite(trigPin, HIGH); //set trigger pin to HIGH
digitalWrite(trigPin1, HIGH); //set trigger pin to HIGH
delayMicroseconds(20); //delay of 20 microseconds
digitalWrite(trigPin, LOW); //set trigger pin to LOW
digitalWrite(trigPin1, LOW); //set trigger pin to LOW
timeTaken = pulseIn(echoPin, HIGH); //determine distance of wave
Serial.println(timeTaken);
timeTaken1 = pulseIn(echoPin1, HIGH); //determine distance of wave
Serial.println(timeTaken1);
dist0 = (timeTaken*0.0343) / 2; //using timeTaken calc distance of object and assign to variable dist
Serial.println(dist0);
dist1 = (timeTaken1*0.0343) / 2; //using timeTaken calc distance of object and assign to variable dist
Serial.println(dist1);
//Serial.println("Final Debug"); //uncomment these lines while debugging to see distances for each sensor
//Serial.println(dist0);
//Serial.println(dist1);
//Serial.println(dist);
if (dist0 > 0 && dist1 > 0) //if both sensors are recording a distance more than 0cm
{
dist = (dist0 + dist1)/2; //make dist = the average distance
}
else if (dist0 <= 0 || dist1 <= 0) //if either of the readings are 0cm set average dist to 0cm
{
dist = 0;
}
Serial.println(dist);
//sound buzzer at different distances
if (dist >= 70) { //if distance if 1 or 2
tone(buzzer,1500,100); //sound buzzer at 1500HZ and duration of 100ms
delay(2000); //2 second delay
}
else { //otherwise
noTone(buzzer); //no sound
}
if (dist >= 50 && dist < 60 ) { //if distance if 3
tone(buzzer,1500,100); //sound buzzer at 1500HZ and duration of 100ms
delay(1000); //1 second delay
}
else { //otherwise
noTone(buzzer); //no sound
}
if (dist > 40 && dist <50) { //if distance if 4
tone(buzzer,1500,100); //sound buzzer at 1500HZ and duration of 100ms
delay(500); //half second delay
}
else { //otherwise
noTone(buzzer); //no sound
}
if (dist > 30 && dist <= 40 ) { //if distance if 5 or 6
tone(buzzer,2000,80); //sound buzzer at 2000HZ and duration of 80ms
delay(200); //delay 200milliseconds
} else { //otherwise
noTone(buzzer); //no sound
}
if (dist <= 30) { //if distance if 7 or 8
tone(buzzer,2000,100); //sound buzzer at 2000HZ and duration of 80ms
delay(50); //delay 50milliseconds
} else { //otherwise
noTone(buzzer); //no sound
}
Serial.flush();
//if the readings are in range print the distance
if (dist >= 31 && dist <= 200){ //if value of dist is more than 31cm and less than 200cm complete following
String string1 = "Distance: ";
String string2 = "cm ";
String string3 = " "; //blank space to ensure rest of line on LCD shows nothing
String string4 = string1 + dist + string2 +string3; //adding strings together
Serial.println(string4); //print total string to serial
char charBuf1[35]; //create an array for 35 chars
string4.toCharArray(charBuf1, 35); //convert the string to an array of chars to be sent by radio comms
radio.write(&charBuf1, sizeof(charBuf1)); //Write the message to radio comms
delay(100); //print cm on LCD
}
//if the readings are not in range print out of range
else if(dist < 31 || dist > 200 || timeTaken <= 1800 || timeTaken1 <=1800){ //else if function for if any of these are true, print "out of range"
Serial.println("Out of Range "); //print Out of Range and clear rest of LCD
const char text[] = "Out of Range ";
radio.write(&text, sizeof(text)); //Write the message to radio comms
delay(100);
}
} //close buttonIn==LOW loop
} //close void loop
/*
created 17/06/2019
by Joel Gray
graycode.ie
*/