Friday 10 April 2015


Arduino Uno - Range Finder,  RF transmitter and receiver

The radio frequency in this project is 315 Mhz


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


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
}



For the receiver Audrino


Notice that the command LiquidCrystal lcd(12, 8, 5, 4, 3, 2); is using pin 8 rather than 11, this is because pin 11 is used by VirtualWire.

/*
This sketch displays text strings received using VirtualWire
Connect the Receiver data pin to Arduino pin 11
*/
#include <VirtualWire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 8, 5, 4, 3, 2);

byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message

void setup()
{
      Serial.begin(9600);
      Serial.println("Receiver waiting");
      
      lcd.begin(16, 2);
      lcd.clear();
      lcd.print("Receiver waiting");
      
      // Initialize the IO and ISR
      vw_setup(2000); // Bits per sec
      vw_rx_start(); // Start the receiver
}

void loop()
{
      if (vw_get_message(message, &messageLength)) // Non-blocking
      {
            Serial.print("Received: ");
            
            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print("Received: ");
            lcd.setCursor(0, 1);
            
            for (int i = 0; i < messageLength; i++)
            {
                  Serial.write(message[i]);
                  lcd.print((char)message[i]);
            }
  
            Serial.println();
      }
}


No comments:

Post a Comment