DEV Community

Cover image for Particle Photon 101 - Getting Started Guide
Alvaro Saburido
Alvaro Saburido

Posted on • Updated on • Originally published at alvarosaburido.com

Particle Photon 101 - Getting Started Guide

I found Particle.io (formerly Spark) Photo board a long time ago meanwhile playing around with IoT boards and I was actually surprised with it for 2 reasons:

  • Price (19 \$ against the 27,90 of the closest option in that time, the Arduino MKR WIFI 1010)
  • It brings its own cloud solution.

Particle Photon kit

So I tried the Photon Kit which for 29\$ I got:

  • A nice package (designers got a plus here for product design)
  • A Photon board with headers
  • A solderless breadboard ready to go (Really nice quality)
  • 2 Resistors of 1Kฮฉ
  • 1 RGB Led
  • 1 Phototransistor
  • 1 USB Cable

Also, It brings a really funny card with pinouts where you can insert the actual components on top of the breadboard which was, quite interesting as user experience.

The purpose of this tutorial is to help you get started with your board in no-time. We are going to cover from the initial setup to reading data and controlling LEDs trough the Internet.

Features

  • Particle Pร˜ Wi-Fi module
    • Broadcom BCM43362 Wi-Fi chip
    • 802.11b/g/n Wi-Fi
    • STM32F205RGY6 120Mhz ARM Cortex M3
    • 1MB flash, 128KB RAM
  • On-board RGB status LED (ext. drive provided)
  • 18 Mixed-signal GPIO and advanced peripherals
  • Open source design
  • Real-time operating system (FreeRTOS)
  • Soft AP setup
  • FCC, CE and IC certified

I will not go depth in every aspect of the board itself only for the necessary to get started with it. For more detailed documentation please refer to the official docs here

Setting Up the kit

As soon as you connect the board to the computer with the micro USB cable, you will notice the nice RGB LED on your device is blinking blue (in case it's not hold down the SETUP button), this is called Listening Mode in which your device is waiting for your input to connect to Wi-Fi.

Next thing you need to do is go to setup.particle.io

Setup Particle board

Once you select your board (in our case the Photon), it will prompt a page like this where you can download the local setup file:

Local Setup File

This will download and .html file with the rest of the setup, once you open it will ask you to connect your computer to the Photon's Hotspot.

Connect to your Photon's hotspot

Once connected if will ask you to setup the Wifi credentials of the network you wish your particle device to connect to. (If you mistyped your credentials, the Photon will blink dark blue or green. You have to go through the process again).

Now the most important part of all, you have to select an awesome name for your device to be known in the cloud (please don't add a cheesy name unless you want your device to get bullied in school ๐Ÿค“).

For now, mine will be called awesome-squirtle.

Squirtle

The console

Now that we have our Photon device up and running, let's go to the Particle Cloud console to see what this awesome platform has to offer console.particle.io

Your Particle Console

Once inside you will see a list of your current devices and the status from them. If the Photon is connected, a blue led shining there. Also, click on your device to see a proper dashboard with all the specs:

  • Device ID and status.
  • Signaling.
  • Vitals (RF Signal Power, Memory).
  • OTA (Firmware updates over the cloud).
  • Functions.
  • Variables.

Your Particle Console

This is very powerful, but until now, we only have been configuring and checking pages, I think it's time to do some coding and check the features of this console has, so let's get our test circuit running before we touch any line of code.

The circuit

Circuit_diagram

  • Led + connected to Photon's D0.
  • Led - to a 220ฮฉ resistor connected to Photon's GND.
  • Photoresistor connected to Photon's A5.
  • Photon's A0 connected to the other leg of the photoresistor + 220ฮฉ resistor to GND.

The code

There are two ways to code your photon:

  1. Using the Web Editor
  2. Locally using Particle Workbench or the Workbench Plugin for Visual Studio (it's pretty much the same thing)

In this tutorial, we will use the Particle Build Web IDE, but in further articles, I will explain how to use the second one.

So enter https://build.particle.io/ to get familiar with the online editor and get ready to code.

Particle's code has the same base as Arduino, coding in .ino which is based on C++, so if you ever code in Arduino this should be really straightforward, every sketch has two parts:

void setup() {
  pinMode(led,OUTPUT);
}
Enter fullscreen mode Exit fullscreen mode

A method called setup() where we normally setup the inputs/outputs configuration along with any other initialization.

void loop() {
   digitalWrite(led,HIGH);
   delay(500);
}
Enter fullscreen mode Exit fullscreen mode

Which as its name suggests, is an infinite loop that will run as long as the device is powered with all the instructions and the logic, allowing your program to change and respond.

As soon as you open the Web IDE you will see some example apps on the panel left, please select the one called Function Variable. and let's go through it to explain how it works.

In the top part you will have the global variables (just above the setup method):

int led = D0;
int photoresistor = A0;
int power = A5;
int analogvalue;
Enter fullscreen mode Exit fullscreen mode

Let's align this code with our circuit diagram above, Digital 0 or D0 is where we have our LED connected so assign it to the variable led.

For the photoresistor we have two ends, one to the Analog 0 or A0 that will take the voltage reading and the A5 that will work as a steady voltage source.

We define analogvalue to hold the voltage from A0.

Even if I'mm explaining it here with my own words, the code in the Web IDE itself is really well commented and self-explanatory.

Next we go into the setup function:

void setup() {
  pinMode(led,OUTPUT);
  pinMode(photoresistor,INPUT);
  pinMode(power,OUTPUT);
  ...
}
Enter fullscreen mode Exit fullscreen mode

We use pinMode with the variables we defined before to set the pins that will be inputs (A0) and outputs (A5, D0).

Because we want the photoresistor to have consistent power, we set A5 to have a 5V output digitalWrite(power,HIGH);. In this part you might be wondering, if A5 is analogic, why we are using digitalWrite() method? Because pins from A0-A7 are A/D (Analog to Digital inputs, so if they are set as an OUTPUT they behave like any other digital pin (0 or 5V). For using analogWrite() D/A (Digital to analogic) you will need to use the DAC or DAC1 pin.

Ignore the following two lines using the Particle library, we will come back to them in a moment. Let's go to the loop

void loop() {
    analogvalue = analogRead(photoresistor);
}
Enter fullscreen mode Exit fullscreen mode

We check to see what the value of the photoresistor is and store it in the variable analogvalue (0 to 4095), if you wish to have the actual voltage value, you will need to multiply the analog value by 5V and divide it by the max level possible (4095):

void loop() {
    analogvalue = analogRead(photoresistor);
    voltage = analogvalue * 5.0 / 4095;
}
Enter fullscreen mode Exit fullscreen mode

To flash your device check the left panel on the editor.

Particle Flash

Particle Functions

Until now it was pretty much similar to any other device, let's use some of the cool features this Particle Cloud has to offer. In the setup method add the following:

void setup() {
   ...
  Particle.function("led",ledToggle);
}
Enter fullscreen mode Exit fullscreen mode

Here, we're defining the name of the cloud function we want to create and the callback function, in this case, because we want to control the led over internet, let's do it:

int ledToggle(String command) {

    if (command=="on") {
        digitalWrite(led,HIGH);
        return 1;
    }
    else if (command=="off") {
        digitalWrite(led,LOW);
        return 0;
    }
    else {
        return -1;
    }

}
Enter fullscreen mode Exit fullscreen mode

Depending on the command we write in the console we choose to ON/OFF the led. Let's flash the sketch to our device and go to the console.

Console  function

Try writing down on and off to see your led toggle.

It's Alive

Particle Variables

In the same way you can call functions vรญa the Particle Console, you can also declare variables to read from your device remotely and expose them trough the cloud, so let's track our Photoresistor value and voltage. More info here

float voltage
void setup() {
  ...
  Particle.variable("analogvalue", &analogvalue, INT);
  Particle.variable("voltage", voltage);
  ...
}
Enter fullscreen mode Exit fullscreen mode

Flash it into your device and refresh your console, now, just below Functions you will find your variables, press GET to obtain the recent value.

Now switch on and off the led and check how the values on the photoresistor change:

Console Variables

And that's it for today's tutorial, in the next ones I will explain you more amazing stuff about this product and the cloud, more hardware examples.

If you have any questions feel free to write me in the comments ๐Ÿ˜. Happy coding!

Top comments (0)