Photon Distance Sensor - toSvenson/intro-workshop GitHub Wiki
In this experiment you are going to build a sensor able to measure distances from 10cm up to 200cm. The data will be transmitted via Wi-Fi to a cloud service.
To build this sensor you need the following hardware:
- 1x breadboard
- 1x Particle Photon
- 1x HC-SR04 sensor
- 1x 470 Ohm resistor
- 1x 1k Ohm resistor
- 4x male-male wires
Place components as shown in the schema. You can plug the HC-SR04 sensor directly on the breadboard, making the blue wires unnecessary.
The HC-SR04 sensor is a 5V device while the Photon operates at 3.3V.
Therefor we take provide power to the sensor from the Vin pin (= 5V from USB).
The 2 resistors function as a voltage divider to convert 5V coming from the sensor to a safe 3.3V level.
R1: 470 Ohm (yellow purple black black [brown])
R2: 1k Ohm (brown black black brown [brown])
(If your Photon is already connected, skip to next step)
Each Photon has a unique number associated with it, this number (representing the device) can be easily CLAIMED (and DELETED) from your account.
There are different ways to connect your Photon to your account.You are going to perform the setup using the CLI (Command Line Interface) to fully understand what is happening.
( Make sure you have successfully completed all steps of the Laptop Setup )
OS X users, open a new terminal or Windows users, open a new Node.js command prompt
It's time to connect your Photon to a USB port on your computer.
After a few seconds, the status LED should start blinking blue. If this is not the case, press the SETUP button for 3 seconds (until the led starts blinking blue)
First you want to know the Device ID of the Photon you just connected.
$ particle identify
Your device id is 3c0123456789012345678901
Copy this device id in your clipboard, you will need it in a few moments.
Before we can CLAIM the Photon, we need to connect it to the internet through Wi-Fi.
$ particle setup wifi
? Should I scan for nearby Wi-Fi networks? Yes
? Select the Wi-Fi network with which you wish to connect your device: YourWifiNetwork
? Security Type: WPA2
? Wi-Fi Password: YourWifiPassword
Done! Your device should now restart.
Check the Photon status LED, it should be BREADING CYAN when it's happily connected to the internet.
Now it is time to CLAIM your device and give it a NAME. This will associate it exclusively to your Particle account.
$ particle cloud claim 3c0123456789012345678901
Claiming device 3c0123456789012345678901
Successfully claimed device 3c0123456789012345678901
$ particle cloud name 3c0123456789012345678901 PhotonSonar
Renaming device 3c0123456789012345678901
Successfully renamed device 3c0123456789012345678901 to: PhotonSonar
Check if the device is associated to your account
$ particle cloud list
PhotonSonar [3c0123456789012345678901] (Photon) is online
Functions:
int digitalread(String args)
int digitalwrite(String args)
int analogread(String args)
int analogwrite(String args)
If you see these 4 functions above, you can already send a command to the Photon!
$ particle call PhotonSonar digitalWrite "D7,HIGH"
1
Congratulations, you have completed your first IoT command!
Did you notice what happend with your Photon? The blue LED is connected to D7 (Digital port 7) and you have set it the HIGH making the LED light up. Now try to turn it off again.
$ particle call PhotonSonar digitalWrite "D7,LOW"
1
Very good! You are ready to write your first sketch.
If you can't get enough.. A complete overview of the Particle CLI can be found here.
Open the online build environment of Particle.io on build.particle.io.
At the left-hand side you can see the vertical menu which you will use during the process. Hover you mouse over each item to get familiar with the different options:
FLASH VERIFY SAVE CODE LIBRARIES DOCS DEVICES SETTINGS
- First we want to select the Photon we connected to our account in the previous step. From the menu, press
DEVICES - Check if your device is listed and select it by clicking the yellow star in front of the device name.
- From the menu, press
"< >" Codeto open the code editor - Press
CREATE NEW APPand enter a Title (e.g.PhotonSonarApp) - Again at the left-hand side press
Libraries(first item below"< >") - In the Community Libraries search box type:
HC_SR04 - Press on the search result
HC_SR04 - Press
INCLUDE IN APP, select the app you just created (e.g. photon_temp) and pressADD TO THIS APP - Paste the code below in the
.inofile, replacing it's current content
// This #include statement was automatically added by the Particle IDE.
#include "HC_SR04/HC_SR04.h"
double cm = 0.0;
int trigPin = D4;
int echoPin = D5;
HC_SR04 rangefinder = HC_SR04(trigPin, echoPin);
void setup()
{
Serial.begin(9600);
pinMode(D7, OUTPUT);
Particle.variable("cm", &cm, DOUBLE);
}
void loop()
{
cm = rangefinder.getDistanceCM();
Serial.printf("Distance: %.2f cm\n", cm);
//// Uncomment for extra experiment with Distance sensor
//setRemoteServo(cm);
blinkLed();
delay(850);
}
void blinkLed() {
digitalWrite(D7,HIGH);
delay(150);
digitalWrite(D7,LOW);
}
//// Uncomment for extra experiment with Distance sensor
//void setRemoteServo(double cm) {
// int newValue = (int) cm;
// if (newValue > 50) {
// newValue = 50;
// } else if (newValue == -1) {
// newValue = 10;
// }
//
// int servoPos = map(newValue, 10, 50, 179, 1);
// Particle.publish("SetServoSon", String(servoPos));
//}
- From the menu, press
VERIFY.
This should result in the messageCode verified! Great work. - Now you are ready to press
FLASH
If everything goes well, you will get a:
Flash successful! Please wait a moment while your device is updated... - The Photon device should return to
breading cyanstatus color
Debug readings from local serial port (Photon need to be connected to USB)
$ particle serial monitor
Opening serial monitor for com port: "COM12"
Distance: 196.03 cm
Monitor the values published to the cloud (CLI)
$ particle monitor <device ID>
polling server to see what devices are online, and what variables are available
? Which variable did you want? cm (double)
Hit CTRL-C to stop!
195.60344827586206