User Tools

Site Tools


oak:tutorials:ultrasonic

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
oak:tutorials:ultrasonic [2016/07/29 10:26]
brohan
oak:tutorials:ultrasonic [2017/01/10 00:04]
Rover#18 [Components] cross-referencing to tutorial for HC-SR04
Line 1: Line 1:
-===== Oak: using an Ultrasonic Sensor=====+===== Oak: Using an Ultrasonic Sensor=====
  
-** Currently a Work in Progress ** 
  
 +{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​us-100_ultrasonic_2.png?​300|
 +{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​us-100_ultrasonic_2.png?​500}}
  
-{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​us-100_ultrasonic_2.png?​200| +In this lesson, we will connect an ultrasonic sensor to the Oak. An ultrasonic sensor, using the properties of sound, not only detects an object, but also detects how far away the object is. The sensor generates a high frequency sound, then listens for an echo received back to the sensor. The time interval from send to receive ​allows you to calculate the distance.
-{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​us-100_ultrasonic_2.png?​400}}]] +
- +
-In this lesson, we will connect an ultrasonic sensor to the Oak. An ultrasonic sensor allows you to detect+
  
 ===== Components ===== ===== Components =====
Line 14: Line 12:
 | Oak with soldered headers |1| | | Oak with soldered headers |1| |
 | Breadboard|1| | | Breadboard|1| |
-|Ultrasonic Sensor|1| |+|Ultrasonic Sensor|1|US-100 Y401|
 |Jumper wires|6| | |Jumper wires|6| |
  
  
-**Note:** The HC-SR04 sensor is NOT being used in this tutorial as it uses 5v. Instead a US-100 sensor, which operates ​with 3.3v is used. +**Note:** The HC-SR04 sensor is __**NOT**__ being used in this tutorial as it uses 5v. Instead a US-100 sensor, which operates ​from 3v - 5v is used.  However, two methods for connecting the more common HC-SR04 range sensor are provided in a separate tutorial, [[https://​digistump.com/​wiki/​oak/​tutorials/​ultrasonic_2|here]].
  
-Particle.publish will be used send the sensor data to the cloud. To view the data, you will need free Particle account, which you can get here if you haven't already: [[https://​build.particle.io/​signup]].+Particle.publish will be used to send the sensor data to the cloud. To view the data, you will need your free Particle account. You most likely created one when you first set up your Oak. If you don'​t ​have one already ​get yours here: [[https://​build.particle.io/​signup]].
  
 ===== Concepts ===== ===== Concepts =====
  
 +==== pulseIn ====
 +
 +pulseIn() can be thought of as a specific timer. pulseIn() takes at least 2 parameters, and up to 3, which are: pin, HIGH or LOW, and timeout. pulseIn() reads the pulse width value for the designated pin during a signal HIGH or LOW condition (designated by the state parameter). Pulse width value is the length of time the pin’s voltage remains HIGH or LOW.
 +
 +For example, when the state parameter is set to HIGH, the pulseIn() function waits for the pin’s voltage change to HIGH and measures the length of time the pin’s voltage remains HIGH until it changes to LOW. The length of time is measured in millisecond. The maximum pulse width is 71 minutes. If nothing is detected within the specified time-out period, the function abandons the read attempt and returns 0.
 +
 +==== Particle.publish ====
 +
 +The Particle.io cloud offers many features that Oak users can take advantage of, some of which are quite in depth, and allow various devices to "​communicate"​ with each other by passing information (like Particle.variable). If you just need something simple, such as a notification when something has happened, Particle.publish() may be what your looking for. 
 +
 +Particle.publish() can either be public (available to anyone who has the event name and uses Particle.subscribe) or private (only visible to you, your devices, etc). 
 +
 +Particle.publish() takes the name of the event, the data (char or String), TTL (length of time the data is good for), and if its private.
 +
 +
 +===== Circuit =====
 +
 +The US-100 Ultrasonic sensor has a number of configurations available. In this example we will be configuring it for digital (PWM) mode. In order to do so first remove the jumper on the back of the sensor (with the jumper engaged, the sensor will be in analog mode). The rest of the connections are straight forward: ​
 +
 +  *      Connect the VCC pin to the Oak VCC
 +  *      Connect both GND pins to the Oak GND (via GND rail)
 +  *      Connect the Trig pin to Oak pin #2
 +  *      Connect the Echo pin to Oak pin #4
 +
 +{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​us-100_ultrasonic_2.png?​300|
 +{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​us-100_ultrasonic_2.png?​500}}
 +     
 +
 +Here's a picture of the actual setup. Note the differences from the sketch: The sensor is facing outward from the breadboard, and the Oak is rotated 180 degrees. You can also see here that the jumper does __**NOT**__ connect the 2 rear pins, thereby enabling digital mode.
 +
 +
 +{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​ultrasonic_actual.jpg?​300|
 +{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​ultrasonic_actual.jpg?​500|}}
 +
 +===== Code =====
 +
 +The first part of our sketch is the variable declaration:​
 +
 +<​code>​
 +/*
 + ​US-100 Ultrasonic distance sensor:
 + VCC to Oak vcc
 + GND x2 to Ground Rail to Oak Ground
 + Trig to Oak Pin 2
 + Echo to Oak Pin 4
 + */
 +
 +
 +int trigPin = 2; // Trigger Pin
 +int echoPin = 4; // Echo Pin
 +int LEDPin = 1; // Onboard LED
 +
 +unsigned long Time_Echo_us = 0; // container for PulseIn data
 +unsigned long Len_mm ​ = 0; // variable for length conversion
 +
 +char post_Distance[16];​ //particle post variable
 +
 +</​code>​
 +
 +Next we tell the Oak what the pins will be doing in the setup section:
 +
 +<​code>​
 +
 +void setup() {
 + 
 + ​pinMode(trigPin,​ OUTPUT);
 + ​pinMode(echoPin,​ INPUT);
 + ​pinMode(LEDPin,​ OUTPUT); // Use onboard LED for visual feedback
 + 
 +}
 +
 +</​code>​
 +
 +Now the loop portion. First we tell the sensor to send a sound:
 +
 +<​code>​
 +void loop() {
 +
 + //​Make sure the trigger pin is off
 + ​digitalWrite(trigPin,​ LOW); 
 + ​delay(100); ​
 +
 +//tell the sensor to send its pulse 
 + ​digitalWrite(trigPin,​ HIGH);
 + ​delay(10); ​
 + 
 + //end trigger
 + ​digitalWrite(trigPin,​ LOW);
 +
 +</​code>​
 +
 +Then listen for the echo:
 +
 +<​code>​
 +
 +// pulseIn HIGH starts measuring the time until it hears the echo
 +Time_Echo_us = pulseIn(echoPin,​ HIGH);  ​
 + 
 +// Calculate distance to object. Divide by 2 because echo time is roundtrip
 + ​Len_mm = (Time_Echo_us*34/​100)/​2;​
 +
 +</​code>​
 +
 +Now that we have our data, let's use it:
 +
 +<​code>​
 +//Send distance to cloud for viewing by Particle dashboard, and OakTerm
 + ​sprintf(post_Distance,​ "​%04d",​ Len_mm); //convert int to string for Particle.publish
 +  Particle.publish("​distance in mm:", post_Distance);​ //invoke Particle.publish
 + 
 +/*
 +Use onboard LED for visual feedback, adjust values based on what you see via Particle.publish
 +Could attach an RGB LED to change colors based on distance ​          
 +*/
 + ​if((Len_mm < 3000) && (Len_mm > 1)) {   // Length effective range (1, 3000).
 +  ​
 +digitalWrite(LEDPin,​ HIGH); ​
 + }
 + else { 
 + ​digitalWrite(LEDPin,​ LOW); 
 + ​} ​                          
 +   
 + ​delay(1000); ​                               ​
 + 
 +}
 +</​code>​
 +
 +Our setup in action:
 +
 +{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​ultrasonic.gif?​200|
 +{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​ultrasonic.gif?​400}}
 +
 +===== Conclusion =====
 +
 +You did it. You properly connected, and used an ultrasonic sensor to your Oak, and sent the data to the cloud. Make sure that you log in, and visit your Oak's logs at [[https://​dashboard.particle.io/​user/​devices]]. If you have registered multiple Oaks (why wouldn'​t you have more than one?), be sure to view the logs for the correct Oak.
 +
 +Look into other Particle functions at your disposal, such as Particle.variable,​ that allow you to post your variable, and even access it with another (read 2nd or 3rd oak) device.
  
 +What can **you** use an ultrasonic sensor for?
 +  * "​eyes"​ to keep a robot from running into something
 +  * Connect an RGB, put in your garage for a parking distance guide
oak/tutorials/ultrasonic.txt · Last modified: 2017/01/10 00:04 by Rover#18