Arduino Uno - Range Finder, RF transmitter and receiver
Overview
The range finder sends out a sonic (ping) which bounces off a solid object, it then compares the time taken for the (echo) to be received. All this hard work is taken care of by the HC SR04 ultrasonic module. The transmitting Arduino reads the sensor every two seconds, then uses the VirtualWire library to encode the centimetre value and RF transmitter to broadcast the data at 315 Mhz.
The RF receiver on the second Aurdino is listening on the 315 Mhz channel and picks up the transmission which it then decodes and displays on the Liquid Crystal Display.
The 'clicking' on the video is RF interference from the transmitter!
Shopping list hardware
- 2 * Arduino Uno
- 1 * HC SR04 ultrasonic module (RF Transmitter and RF Receiver)
- 1 * LCD
- 2 * 10k ohm resisters
Aurdino software and libraries
- Arduino Development Environment I'm using v1.6.3
- VirtualWire to control the HC SR04 ultrasonic module
- Liquid Crystal (this will already be in your Arduino Library folder)
Receiver circuit
See the official Arduino wiring diagram for the LCD, the only difference is that pin 8 is used instead of pin 11 (as 11 is needed for the receiver). For more information on the HC SR04 see this very good overview page.
HC SR04 wiring
VCC to 5v
GND to Ground
Data to pin 11
HC SR04 wiring
VCC to 5v
GND to Ground
Data to pin 11
Transmitter circuit
For the transmitter Audrino
#define trigPin 9#define echoPin 8
#include <VirtualWire.h>
char strDistance[6];
void setup()
{
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
send("Meter is ready");
}
void loop()
{
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
send("Out of range");
} else {
Serial.print(distance);
Serial.println(" cm");
sprintf(strDistance, "%i cm", distance);
send(strDistance);
}
delay(2000);
}
void send (char *message)
{
vw_send((uint8_t *)message, strlen(message));
vw_wait_tx(); // Wait until the whole message is gone
}