Hello again,
This is my another post in which I am going to cover Bluetooth and Ultrasonic sensor(HC-SR04).
Now the question arises,Why I have used these two.I made an RC 2 wheel car for my school project. It is controlled with a free app via bluetooth. To make it "half robot", it has distance sensor, that measures distance. If the distance is (in my case) equal or lower than 10cm, the car stops and after 1.5s the person can control it again.
I will add code at the end of this post.You can go through that code.If you have any problem regarding this,you can comment on the comment section.
Step 1: THE HC-SR04 ULTRASONIC SENSOR
First of all,I am going to tell you about the HC_SR04 ultrasonic sensor.
This component is very simple, as it only has 4 pins.
First and the last are power pins Vcc and GND. The middle ones are Trigger pin and Echo pin.
If you want to understand what these pins do, you must understand how this thing works.
So,this module is sending ultrasonic waves then these waves reflect from a surface and come back to the module.Arduino is triggering those waves on the trigger pin and than listening for the “echo”. Once it receives it, it calculates the distance based on the time spend waiting for the wave to come back. In the code, we are also going to change that value to cm, as it it easier to read.
The module will be connected to any I/O pins of the arduino.The trigger pin will be output and the echo pin will be input.
I will also describe how the code works in the programming step.
Step 2: PROGRAMMING THE ULTRASONIC SENSOR
int triggerPin = 7; //triggering on pin 7
int echoPin = 8; //echo on pin 8
void setup()
{
Serial.begin(9600); //we'll start serial comunication, so we can see the distance on the serial monitor
pinMode(triggerPin, OUTPUT); //defining pins
pinMode(echoPin, INPUT);
}
void loop()
{
int duration, distance; //Adding duration and distance
digitalWrite(triggerPin, HIGH); //triggering the wave(like blinking an LED)
delay(10);
digitalWrite(triggerPin, LOW);
duration = pulseIn(echoPin, HIGH); //a special function for listening and waiting for the wave
distance = (duration/2) / 29.1; //transforming the number to cm(if you want inches, you have to change the 29.1 with a suitable number
Serial.print(distance); //printing the numbers
Serial.print("cm"); //and the unit
Serial.println(" "); //just printing to a new line
//IF YOU WANT THE PORGRAM SPITTING OUT INFORMATION SLOWER, JUST UNCOMMENT(DELETE THE 2 //) THE NEXT LINE AND CHANGE THE NUMBER
//delay(500);
}
Step 3: THE BLUETOOTH MODULE
First of all,I am going to tell you the Bluetooth module.Probably all Bluetooth modules have the same pin-out.
It has power pins Vcc and GND and communication pins RX and TX.
Now you can probably see, that this module connects to the RX and TX pins of a micro controller such as Arduino or a USB to serial converter.
This module works fully with 5V, so there is no problem connecting it to Arduino. All the pins are labeled on the back of the module.
You can see the connections in the images.
Step 4: PROGRAMMING THE BLUETOOTH MODULE
The code is made for turning LED on and OFF, but with the same method of "else if" statements seen in the code, you can do enything.
Another thing is that When you have your bluetooth module connected to the TX and RX pins to the arduino, remember to pull them out every time you' re uploading the sketch or it will display an error like on the picture 2, than disconnect the RX and TX pins from the bluetooth module and upload the code again. Once the code is uploaded,connect the pins again.
After all that, you need to connect your phone to the bluetooth module. Power the arduino(and at the same time the bluetooth module) and find it in. Once you added the device, download this app for Android.I don't know if this works with Apple devices.
The app used is:https://play.google.com/store/apps/details?id=eu.jahnestacado.arduinorc, so it's called Arduino RC
int LED = 13; //led pin
int info = 0;//variable for the information comming from the bluetooth module
int state = 0;//simple variable for displaying the state
void setup()
{
Serial.begin(9600); //making serial connection
pinMode(LED, OUTPUT); //defining LED pin
digitalWrite(LED, LOW); //once the programm starts, it's going to turn of the led, as it can be missleading.
}
void loop()
{
if(Serial.available() > 0){ //if there is any information comming from the serial lines...
info = Serial.read();
state = 0; //...than store it into the "info" variable
}
if(info == '1')
{ //if it gets the number 1(stored in the info variable...
digitalWrite(LED, HIGH); //it's gonna turn the led on(the on board one)
if(state == 0)
{ //if the flag is 0, than display that the LED is on and than set that value to 1
Serial.println("LED ON"); //^^that will prevent the arduino sending words LED ON all the time, only when you change the state
state = 1;
}
}
else if(info == '0')
{
digitalWrite(LED, LOW); //else, it's going to turn it off
if(state == 0)
{
Serial.println("LED OFF");//display that the LED is off
state = 1;
}
}
}
This is my another post in which I am going to cover Bluetooth and Ultrasonic sensor(HC-SR04).
Now the question arises,Why I have used these two.I made an RC 2 wheel car for my school project. It is controlled with a free app via bluetooth. To make it "half robot", it has distance sensor, that measures distance. If the distance is (in my case) equal or lower than 10cm, the car stops and after 1.5s the person can control it again.
I will add code at the end of this post.You can go through that code.If you have any problem regarding this,you can comment on the comment section.
Step 1: THE HC-SR04 ULTRASONIC SENSOR
First of all,I am going to tell you about the HC_SR04 ultrasonic sensor.
This component is very simple, as it only has 4 pins.
First and the last are power pins Vcc and GND. The middle ones are Trigger pin and Echo pin.
If you want to understand what these pins do, you must understand how this thing works.
So,this module is sending ultrasonic waves then these waves reflect from a surface and come back to the module.Arduino is triggering those waves on the trigger pin and than listening for the “echo”. Once it receives it, it calculates the distance based on the time spend waiting for the wave to come back. In the code, we are also going to change that value to cm, as it it easier to read.
The module will be connected to any I/O pins of the arduino.The trigger pin will be output and the echo pin will be input.
I will also describe how the code works in the programming step.
Step 2: PROGRAMMING THE ULTRASONIC SENSOR
int triggerPin = 7; //triggering on pin 7
int echoPin = 8; //echo on pin 8
void setup()
{
Serial.begin(9600); //we'll start serial comunication, so we can see the distance on the serial monitor
pinMode(triggerPin, OUTPUT); //defining pins
pinMode(echoPin, INPUT);
}
void loop()
{
int duration, distance; //Adding duration and distance
digitalWrite(triggerPin, HIGH); //triggering the wave(like blinking an LED)
delay(10);
digitalWrite(triggerPin, LOW);
duration = pulseIn(echoPin, HIGH); //a special function for listening and waiting for the wave
distance = (duration/2) / 29.1; //transforming the number to cm(if you want inches, you have to change the 29.1 with a suitable number
Serial.print(distance); //printing the numbers
Serial.print("cm"); //and the unit
Serial.println(" "); //just printing to a new line
//IF YOU WANT THE PORGRAM SPITTING OUT INFORMATION SLOWER, JUST UNCOMMENT(DELETE THE 2 //) THE NEXT LINE AND CHANGE THE NUMBER
//delay(500);
}
Step 3: THE BLUETOOTH MODULE
First of all,I am going to tell you the Bluetooth module.Probably all Bluetooth modules have the same pin-out.
It has power pins Vcc and GND and communication pins RX and TX.
Now you can probably see, that this module connects to the RX and TX pins of a micro controller such as Arduino or a USB to serial converter.
This module works fully with 5V, so there is no problem connecting it to Arduino. All the pins are labeled on the back of the module.
You can see the connections in the images.
Step 4: PROGRAMMING THE BLUETOOTH MODULE
The code is made for turning LED on and OFF, but with the same method of "else if" statements seen in the code, you can do enything.
Another thing is that When you have your bluetooth module connected to the TX and RX pins to the arduino, remember to pull them out every time you' re uploading the sketch or it will display an error like on the picture 2, than disconnect the RX and TX pins from the bluetooth module and upload the code again. Once the code is uploaded,connect the pins again.
After all that, you need to connect your phone to the bluetooth module. Power the arduino(and at the same time the bluetooth module) and find it in. Once you added the device, download this app for Android.I don't know if this works with Apple devices.
The app used is:https://play.google.com/store/apps/details?id=eu.jahnestacado.arduinorc, so it's called Arduino RC
int LED = 13; //led pin
int info = 0;//variable for the information comming from the bluetooth module
int state = 0;//simple variable for displaying the state
void setup()
{
Serial.begin(9600); //making serial connection
pinMode(LED, OUTPUT); //defining LED pin
digitalWrite(LED, LOW); //once the programm starts, it's going to turn of the led, as it can be missleading.
}
void loop()
{
if(Serial.available() > 0){ //if there is any information comming from the serial lines...
info = Serial.read();
state = 0; //...than store it into the "info" variable
}
if(info == '1')
{ //if it gets the number 1(stored in the info variable...
digitalWrite(LED, HIGH); //it's gonna turn the led on(the on board one)
if(state == 0)
{ //if the flag is 0, than display that the LED is on and than set that value to 1
Serial.println("LED ON"); //^^that will prevent the arduino sending words LED ON all the time, only when you change the state
state = 1;
}
}
else if(info == '0')
{
digitalWrite(LED, LOW); //else, it's going to turn it off
if(state == 0)
{
Serial.println("LED OFF");//display that the LED is off
state = 1;
}
}
}
Once you downloaded the app, run it and click connect to device. Choose your bluetooth module(my is the first one) and you will be taken to "the lobby". If you have noticed, this app is for RC(remote control) which is perfect for controllng a car. It has buttons or gyro sensoring. But what makes it not only for RC is the "TERMINAL MODE".There you can type in anything that will be sent to the arduino. In our case, If you send 1, the LED will come on, or if you type 0, the LED will go off.
Step 6: COMBINING DEVICES
Now, I've combine both of the codes, so you can turn on the LED remote and turn it off for 2s when the distance is less than 15cm.
All I did is that:
- I combined all the int variables above the setups
- I combine both setups into one
- I named each loop of each code "bluetooth" and "sensor"
- In the main loop I added those 2 loops
- I added stopping function in the sensor part.
int triggerPin = 7; //triggering on pin 7
int echoPin = 8; //echo on pin 8
int LED = 13; //led pin
int info = 0;//variable for the information comming from the bluetooth module
int state = 0;//simple variable for displaying the state
void setup()
{ //we will be combinig both setups from the codes
Serial.begin(9600); //we'll start serial comunication, so we can see the distance on the serial monitor
pinMode(triggerPin, OUTPUT); //defining pins
pinMode(echoPin, INPUT);
pinMode(LED, OUTPUT); //defining LED pin
digitalWrite(LED, LOW); //once the programm starts, it's going to turn of the led, as it can be missleading.
}
void loop()
{ //here we combine both codes to work in the loop
bluetooth();
sensor();
}
void bluetooth()
{ //loop from the bluetooth code is renamed to "bluetooth" void
if(Serial.available() > 0)
{ //if there is any information comming from the serial lines...
info = Serial.read();
state = 0; //...than store it into the "info" variable
}
if(info == '1')
{ //if it gets the number 1(stored in the info variable...
digitalWrite(LED, HIGH); //it's gonna turn the led on(the on board one)
if(state == 0)
{ //if the flag is 0, than display that the LED is on and than set that value to 1
Serial.println("LED ON"); //^^that will prevent the arduino sending words LED ON all the time, only when you change the state
state = 1;
}
}
else if(info == '0')
{
digitalWrite(LED, LOW); //else, it's going to turn it off
if(state == 0)
{
Serial.println("LED OFF");//display that the LED is off
state = 1;
}
}
}
void sensor()
{ //loop from the sensor code is renamed to the "sensor" void
int duration, distance; //Adding duration and distance
digitalWrite(triggerPin, HIGH); //triggering the wave(like blinking an LED)
delay(10);
digitalWrite(triggerPin, LOW);
duration = pulseIn(echoPin, HIGH); //a special function for listening and waiting for the wave
distance = (duration/2) / 29.1; //transforming the number to cm(if you want inches, you have to change the 29.1 with a suitable number
Serial.print(distance); //printing the numbers
Serial.print("cm"); //and the unit
Serial.println(" "); //just printing to a new line
//adding for mesuring distance where the led will turn off, even if we tell it to turn off when we chose so in the app
if(distance <= 15)
{ //if we get too close, the LED will turn off, that's the method with my RC car, if it gets too close, it turns off, but that will be in the next instructable :)
digitalWrite(LED, LOW);
Serial.println("TOO CLOSE!!!");
delay(2000); //so the stopping is visabl
}
}
No comments:
Post a Comment