Showing posts with label hcsr04. Show all posts
Showing posts with label hcsr04. Show all posts

Monday, November 3, 2014

Preparing for 2015 after school computer club


Over the past 2 years I have done a Code Club at my kids school using Scratch and last year included a bit of Arduino. Now in the UK computing is part of the school curriculum so the Code Club learning is now done in school time meaning there is not need for it to be done after school.
With this in mind for 2015 I've decided to ramp up the difficulty and go Arduino all the way with a robot course.

This time limited to 10 kids working in pairs as the complexity is higher and the projects will most likely be done over multiple weeks.




This will give the ability to build everything from a pre-programmed robot to a line following robot or an object avoidance robot. Also included an infrared sensor and remote so the kids can make a remote control robot.

All going well the kids will also be able to decorate their robots with unique bodies to make them more exciting.

In addition to the parts below I also have 36 rechargeable AA batteries for the club.

I'm looking forward to January already.

Below are the list of parts I plans to have for the 8-9 week club. 5 kits

ItemeBay LinkPriceQTYTotal
Robot Deckhttp://ebay.to/10NBbMM£7.485£37.40
DC Motor H-Bridgehttp://ebay.to/1GeGZyY£1.555£7.75
Arduino Nano compatiblehttp://ebay.to/1rRgTrh£2.895£14.45
5 x Breadboard 400 tiehttp://ebay.to/1EcNoZs£9.491£9.49
Line following sensorhttp://ebay.to/1wXWDKI£1.195£5.95
10 x Ultra Sonic sensorhttp://ebay.to/1GeHwRl£8.991£8.99
Infrared sensor + Remotehttp://ebay.to/1A40r0N£1.355£6.75
Wires M-Mhttp://ebay.to/1tujL19£1.991£1.99
Wires M-Fhttp://ebay.to/1tSRIKe£1.991£1.99
Wires F-Fhttp://ebay.to/1tSRRxf£2.241£2.24
Keypad 4x4http://ebay.to/1wX1r4F£0.995£4.95
£101.95

Tuesday, May 15, 2012

Arduino - HC-SR04 ultrasonic distance sensor

Last Christmas as part of my stocking fillers I got an HC-SR04 ultrasonic distance sensor.
Like most of my electronic bits it's a cheap generic device from ebay.  It's a small sensor that is suppose to do the same thing as the Ping sensor but for less money.


I finally unwrapped it and found a library for Arduino IDE 1.0 at HERE.  There is even some sample code to read the HC-SR04 and display the results on an LCD display.


Wiring is really simple
VCC - 5V
GND - GND

Trig - Trigger Pin you define in the code
Echo - Echo Pin defined in the code

Since I don't have an LCD display (yet) I modified the code to use the Serial Monitor as the output.
Code Below:

    #include "Ultrasonic.h"

      int TriggerP = 13; // Trigger Pin for Sensor
      int EchoP = 12; // Echo Pin for Sensor
     
      Ultrasonic ultrasonic(TriggerP,EchoP);   
   
       void setup()
    {
      Serial.begin(9600);
    }

    void loop()
    {
      Serial.print("Distance: ");
      Serial.print(ultrasonic.Ranging(CM)); // Get Range in Centimetres
      Serial.println(" Cm.");
      delay(1000);
      Serial.println("..."); // Next lines.
    }

This worked great up to about 16cm.  But beyond that it started to give me some random numbers greater than 4000, so I'd expect not very robust.

I continued my search and found the following code that doesn't use any library, and this code even does the Serial Monitor as the output so no modification needed.


/*
 HC-SR04 Ping distance sensor]
 VCC to arduino 5v GND to arduino GND
 Echo to Arduino pin 13 Trig to Arduino pin 12
 More info at: http://goo.gl/kJ8Gl
 */

#define trigPin 12
#define echoPin 13

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

This code performed better, working out to about 24cm.  
While doing my research on using the HC-SR04 I did find a post (somewhere...) stating that when powering from USB the 5V line can be a little bit off so this might be part of my problem.

But, using either method I have a sensor that is good up to 15cm which could be good enough for a small autonimous robot as if I'm within 15 cms I will need to look around for another direction.

NOTE: If you are using a version of the Arduino IDE before 1.0 then at the following LINK you can get the relevant library.  It looks like it's the same as the 1.0 library and has similar performance.

The hunt is on to see if there is a way of getting a reading beyond 24cm that is reliable.

Loving the tinkering.


UPDATE:
I combined some of the code from the Library with the direct code and now it reliably(ish) without detailed testing will work to 70cm.

The modified code is below.


/*
 HC-SR04 Ping distance sensor]
 VCC to arduino 5v GND to arduino GND
 Echo to Arduino pin 13 Trig to Arduino pin 12
 More info at: http://goo.gl/kJ8Gl
 */

#define trigPin 13
#define echoPin 12

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line

  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

Once again, I love this Tinkering.