This tutorial created by Sudhu Tewari 2023
- from materials originally created by: Michael Shiloh and Judy Castro for Teach Me To Make
The tutorial will focus on getting you up and running with Particle Photon2 quickly, so that you will understand the basic procedures for working with your Particle Photon2
We will cover how to connect your Photon2 to your laptop; how to understand, modify, and write programs (aka code); how to connect input devices and sensors to your Photon2 and read them from a program; and how to connect actuators (LEDs, motors, speakers) and control them from a program. Other topics will be covered as interest dictates and time permits.
- Read about the Photon2: https://docs.particle.io/reference/datasheets/wi-fi/photon-2-datasheet/#functional-description
- Getting Started Dashboard: https://docs.particle.io/getting-started/getting-started/
- create a Particle account particle.io/signup
- follow the steps at setup.particle.io to get your Photon2 set up
- in order to get started it's easiest if you are connected to wifi
- luckily Particle has made it easy to configure wifi for Photon2
- Navigate to Particle Configure Wi-Fi page
- follow the prompts to enter your wifi credentials
- Wi-Fi Network name
- password
- follow the prompts to enter your wifi credentials
Configure Photon 2 to connect with Cal IoT Wi-Fi (Do this at home -> required to use your Photon2 on campus)
- In order to use Cal Wi-Fi with our Photons we must register our device with the Berkeley IoT Wi-Fi network (using its MAC address)
- In order to register the device we must first obtain the MAC address of our device
- The code below asks the device to print its own MAC address (to serial):
- First we need to flash the code to our device.
- go here -> GetMacAddress.ino (right-click: Open Link in New Tab)
- or copy the code below into the Particle Web IDE
void setup() {
Serial.begin();
}
void loop() {
byte mac[6];
WiFi.macAddress(mac);
for (int i=0; i<6; i++)
Serial.printf("%02x%s", mac[i], i != 5 ? ":" : "\r\n");
delay(1000);
}
- We 'll use the Particle Web IDE to flash this code to our device.
- reveal your code sidebar by clicking the <> icon on the left
- copy this app to your own file space
- You should see your device name in the lower right of the Web IDE window
- Click on the lightning bolt icon to flash the code to your device
- you should see some evidence of activity.
- you'll see a spinning arrow animation where the lightnining bolt was
- the RGB LED on your Photon 2 will blink various colors
- text at the bottom of the screen will let you know if the flash is successful
- It's working!!
- but why can't we see anything happening?
- Use the Particle USB serial debug log to monitor the serial output. (right-click: Open Link in New Tab)
- BAM! There's your device's MAC address
XX:XX:XX:XX:XX:XX
- Copy this address somewhere safe and bring it with you to campus so that you can get onto the IoT network
- Navigate to the UC Berkeley Wi-Fi Portal
- under "Berkeley-IoT Wi-Fi Network Devices" click on Manage devices
- click
Create New
- enter in the MAC address you copied in the earlier steps
- issue the device a name
- copy your Berkeley-IoT password somewhere SAFE so that you will remember it!
- this password is only shown once. It will never be shown again...
- Navigate to: https://docs.particle.io/tools/developer-tools/configure-wi-fi/
- follow the prompts to enter your wifi credentials
- Wi-Fi network name (Berkeley-IoT)
- Berkeley-IoT password (generated by UC Berkeley portal in the previous step)'
- follow the prompts to enter your wifi credentials
- Install VSCode: TDF-Software: Installing
- Connect to Berkeley IoT Wi-Fi: Particle: connecting a Photon 2 to the UCB IoT network
-
To keep things simple we'll begin writing code (apps) in the Particle Web IDE
-
Later on we'll use VSCode to get into the really fun stuff.
- Copy the code below into a new app or
- navigate to this page: HelloWorld.ino and copy the app.
/*
Hello World
A "Hello, World!" program generally is a computer program that
outputs or displays the message "Hello, World!".
Such a program is very simple in most programming languages,
and is often used to illustrate the basic syntax of a programming language.
It is often the first program written by people learning to code
*/
void setup() {
//initialize serial communications
Serial.begin();
}
void loop() {
//send 'Hello, world!' over the serial port
Serial.println("hello World");
//wait 1 second between messages so we don't drive ourselves crazy
delay(1000);
-
The Serial commands asks the Photon2 to send a message to your laptop. In order to see this message you'll need to use the Particle USB serial debug log to monitor the serial output. (right-click: Open Link in New Tab)
-
a little code anatomy:
-
the
setup()
function is called when a sketch starts.- Use it to initialize variables, pin modes, start using libraries, etc.
- The
setup()
function will only run once, after each powerup or reset of the Photon2 microcontroller.
-
The
loop()
function does precisely what its name suggests, and loops consecutively through your list of instructions.- Photon2 only executes one instruction at a time.
-
//
indicates a comment line meant for humans to read but ignored by the compiler that turn this text into machine code.- code editors will recognize this and format the text accordingly
-
-
More on specific functions and variables soon! Let's make something happen in the real world first.
use the Particle Tutorial!
https://docs.particle.io/getting-started/hardware-tutorials/hardware-examples/
In order to connect inputs or outputs to your microcontroller you need to know where the GPIO (general-purpose input/output) pins are!
Using a solderless Breadboard to connect your microcontroller to other things (LEDs, motors, speakers, sensors, etc.)
- partial duplicate information of Particle's "Blink an LED hardware examples"
In order to connect inputs or outputs to your microcontroller you need to have a way of making electrical connections!
The Solderless Breadboard
-
LEDs must always be used with resistors so they don’t burn out.
-
The resistor value can be anywhere from 100 ohm to 1k ohm.
- The lower the resistance, the brighter the light.
- Evil Mad Scientist explains it well here
-
Resistor Color Code!
-
-
LEDs are polarized
-
Connect the anode (+) of the LED to pin 17 and
-
Connect the cathode (-) of the LED the anode to one leg of a 220Ω (ohm) resistor
- 220Ω = red - red - brown - gold
-
Connect the other leg of the resistor to ground (GND)
Here’s a picture showing how to connect the LED and resistor on the breadboard:
Here is another view of this circuit, breadboard view in Fritzing:
Here is another view of this circuit, the schematic diagram:
find example code here -> External LED Blink app (right-click: Open Link in New Tab)
-
copy this app to your own file space
-
flash the app to your hardware
You should find that the external LED blink on and off just like the onboard LED was doing.
Yay!
Can we blink both LEDs?
Yes!!
find example code here -> Internal/External LED Blink app (right-click: Open Link in New Tab)
-
copy this app to your own file space
-
flash the app to your hardware
internal and external LED should blink alternately!
-
partial duplicate information of Particle's ["Blink an LED hardware examples"]
- but we're going to do it now withOUT sending data to the cloud because each packet of data we send to the cloud counts towards total monthly use and over a certain amount we have to pay for the data we're sending...
-
Don't remove the LED and resistor from the last step
-
Connect a LDR (light dependent resistor) anywhere on the breadboard
-
Connect one leg of the LDR to 3.3V
-
Connect the other leg of the LDR to A1 (analog input 1) AND one leg of a 10kΩ (kilo-ohm) resistor
- 10kΩ = brown - black - orange - gold
-
Connect the other leg of the 10kΩ resistor to ground (GND)
Once you've got the circuit built head over to the Web IDE and let's write copy some code:
find example code here -> P2T_LED_LDR_V1 (right-click: Open Link in New Tab)
-
copy this app to your own file space
-
flash the app to your hardware
What happens?
hint: Remember the USB Serial Monitor from way back when we had to find the Mac address of our device?
uh oh, that doesn't look right.
what's wrong?
Try this... find example code here -> P2T_LED_LDR_V2 (right-click: Open Link in New Tab)
-
copy this app to your own file space
-
flash the app to your hardware
Download and install the standalone Arduino IDE or try out the Arduino Web IDE
Try copying from the Arduibno examples and porting to Photon2