Arduino Programming 101 - WordPress.com · the Arduino. The other two wires are strictly power. The...

13
http://robotsclub.wordpress.com Arduino Programming 101 Lesson 3: Using an Ultrasonic Sensor

Transcript of Arduino Programming 101 - WordPress.com · the Arduino. The other two wires are strictly power. The...

Page 1: Arduino Programming 101 - WordPress.com · the Arduino. The other two wires are strictly power. The yellow wire is connecting the Trigger to Pin 13 The green wire is connecting the

http://robotsclub.wordpress.com

Arduino Programming 101Lesson 3: Using an Ultrasonic Sensor

Page 2: Arduino Programming 101 - WordPress.com · the Arduino. The other two wires are strictly power. The yellow wire is connecting the Trigger to Pin 13 The green wire is connecting the

http://robotsclub.wordpress.com

Topics to be CoveredWhat You’ll Need

Wiring the Ultrasonic Sensor

Starting from Scratch

Various Variables

Setup the Setup Method

The Loop Method

A Custom Method

The Final Code

Page 3: Arduino Programming 101 - WordPress.com · the Arduino. The other two wires are strictly power. The yellow wire is connecting the Trigger to Pin 13 The green wire is connecting the

http://robotsclub.wordpress.com

What You’ll NeedAn Arduino

A Breadboard (optional)

4 Male-to-Male Jumper Cables

An Ultrasonic Sensor

I will be using a SainSmart HC-SR04 Ultrasonic Sensor

Please note that this code won’t work with all Ultrasonic sensors. I recommend googling other models to find out how to use them with an Arduino

Image from: http://www.sainsmart.com/ultrasonic-ranging-detector-mod-hc-sr04-distance-sensor.html

Page 4: Arduino Programming 101 - WordPress.com · the Arduino. The other two wires are strictly power. The yellow wire is connecting the Trigger to Pin 13 The green wire is connecting the

http://robotsclub.wordpress.com

Wiring the Ultrasonic SensorFollow this WiringDiagram

Connection Key:

Red: 5V > VC

Black: GND > GND

Yellow: 13 > Trig

Green: 12 > Echo

Page 5: Arduino Programming 101 - WordPress.com · the Arduino. The other two wires are strictly power. The yellow wire is connecting the Trigger to Pin 13 The green wire is connecting the

http://robotsclub.wordpress.com

Starting from Scratch

Before I begin, it should be noted that I will be going very quickly through this code. I am assuming you are familiar with the syntax covered in the previous two lessons.

Start with your basic Arduino program

Reference: Examples>01.Basics>BareMinimum

void setup() {!!}!!void loop() {!!}

Page 6: Arduino Programming 101 - WordPress.com · the Arduino. The other two wires are strictly power. The yellow wire is connecting the Trigger to Pin 13 The green wire is connecting the

http://robotsclub.wordpress.com

Various Variables

As per the Wiring Diagram, there are two wires that are connected to digital pins on the Arduino. The other two wires are strictly power.

The yellow wire is connecting the Trigger to Pin 13

The green wire is connecting the Echo to Pin 12

/*! • HC-SR04 Ultrasonic Sensor! • VCC to Arduino 5v GND to! Arduino GND! • Echo to Arduino pin 13 ! • Trig to Arduino pin 12!*/!!#define trigPin 13!#define echoPin 12!

Page 7: Arduino Programming 101 - WordPress.com · the Arduino. The other two wires are strictly power. The yellow wire is connecting the Trigger to Pin 13 The green wire is connecting the

http://robotsclub.wordpress.com

Setup the Setup MethodLine 1: Open a Serial Connection (Allows Arduino to send messages to the computer serial monitor)

Line 2: Set TriggerPin to OUTPUT

Line 3: Set EchoPin to INPUT

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

Page 8: Arduino Programming 101 - WordPress.com · the Arduino. The other two wires are strictly power. The yellow wire is connecting the Trigger to Pin 13 The green wire is connecting the

http://robotsclub.wordpress.com

The Loop MethodLine 1: New Variable

Line 2: Call blinkLED method set result to distance variable

Line 3 - 5: If Block: If distance is over 200 cm or 0 or negative, write out of range to the monitor

Line 6 - 9: Else Block: Print the distance in cm to the monitor

Line 10: Delay for 0.5 seconds

void loop() {! long distance;! distance = blinkLED();! if (distance >= 200 || distance <= 0){! Serial.println("Out of range");! }! else {! Serial.print(distance);! Serial.println(" cm");! }! delay(500);!}

Page 9: Arduino Programming 101 - WordPress.com · the Arduino. The other two wires are strictly power. The yellow wire is connecting the Trigger to Pin 13 The green wire is connecting the

http://robotsclub.wordpress.com

A Custom MethodYou can write your own method and call it from the existing setup or loop methods. This just helps you separate and easily reuse code. Here, instead of void, we write long because our function will be calculating and outputting a number

Line 1: Define 2 variables: duration and dist

Line 2: Make sure trigger pin is not already on

long blinkLED(){! long duration, dist;! digitalWrite(trigPin, LOW); ! delayMicroseconds(2); ! digitalWrite(trigPin, HIGH);! delayMicroseconds(10); ! digitalWrite(trigPin, LOW);! duration = pulseIn(echoPin, HIGH);! dist = (duration/2) / 29.1;! return dist;!}

Page 10: Arduino Programming 101 - WordPress.com · the Arduino. The other two wires are strictly power. The yellow wire is connecting the Trigger to Pin 13 The green wire is connecting the

http://robotsclub.wordpress.com

Line 3: A quick delay of 2 microseconds to make sure everything’s ready

Line 4: Turn the trigger pin on

Line 5: Wait exactly 10 microseconds. During this time the Ultrasonic sensor is emitting an ultrasonic wave

Line 6: Turn off the trigger pin

Line 7: Store the returningsignal from the echo pin using the pulseIn() method

long blinkLED(){! long duration, dist;! digitalWrite(trigPin, LOW); ! delayMicroseconds(2); ! digitalWrite(trigPin, HIGH);! delayMicroseconds(10); ! digitalWrite(trigPin, LOW);! duration = pulseIn(echoPin, HIGH);! dist = (duration/2) / 29.1;! return dist;!}

Page 11: Arduino Programming 101 - WordPress.com · the Arduino. The other two wires are strictly power. The yellow wire is connecting the Trigger to Pin 13 The green wire is connecting the

http://robotsclub.wordpress.com

Line 8: The raw input is the time it took from the emission of the signal to the return of the reflected signal. Dividing it by 2 gives the time the signal took from the sensor to the object in front of it. Then divide by 29.1 to get output in Centimeters.

Line 9: Output the result when the method completes

long blinkLED(){! long duration, dist;! digitalWrite(trigPin, LOW); ! delayMicroseconds(2); ! digitalWrite(trigPin, HIGH);! delayMicroseconds(10); ! digitalWrite(trigPin, LOW);! duration = pulseIn(echoPin, HIGH);! dist = (duration/2) / 29.1;! return dist;!}

Page 12: Arduino Programming 101 - WordPress.com · the Arduino. The other two wires are strictly power. The yellow wire is connecting the Trigger to Pin 13 The green wire is connecting the

http://robotsclub.wordpress.com

The Final Code

Here’s the whole code

Don’t worry if the custom method was a bit confusing. We’ll cover creating custom methods in detail in a future lesson

/* HC-SR04 Ultrasonic Sensor! VCC to Arduino 5v GND to Arduino GND! Echo to Arduino pin 13 ! Trig to Arduino pin 12!*/!#define trigPin 13!#define echoPin 12!void setup() {! Serial.begin (9600);! pinMode(trigPin, OUTPUT);! pinMode(echoPin, INPUT);!}!void loop() {! long distance;! distance = blinkLED();! if (distance >= 200 || distance <= 0){! Serial.println("Out of range");! }! else {! Serial.print(distance);! Serial.println(" cm");! }! delay(500);!}!long blinkLED(){! long duration, dist;! digitalWrite(trigPin, LOW); ! delayMicroseconds(2); ! digitalWrite(trigPin, HIGH);! delayMicroseconds(10); ! digitalWrite(trigPin, LOW);! duration = pulseIn(echoPin, HIGH);! dist = (duration/2) / 29.1;! return dist;!}!/* Code adapted from !http://www.instructables.com/id/Simple-Arduino-and-HC-SR04-Example/!*/

Page 13: Arduino Programming 101 - WordPress.com · the Arduino. The other two wires are strictly power. The yellow wire is connecting the Trigger to Pin 13 The green wire is connecting the

http://robotsclub.wordpress.com

The EndQuestions? Comments? Concerns? Suggestions? Please let me know!