Particle Photon 101 - Getting Started Guide
Tags: particle, photon, iot, arduino
Published: 2020-04-29 07:00:37

I found [Particle.io](https://www.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](https://store.arduino.cc/arduino-mkr-wifi-1010))
- It brings its own cloud solution.

![Particle Photon kit](https://res.cloudinary.com/alvarosaburido/image/upload/v1587824715/blog/Particle%20Photon%20101%20Starting%20guide/photon-kit_mu2mqx.jpg)

So I tried the [Photon Kit](https://store.particle.io/products/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](https://docs.particle.io/datasheets/wi-fi/photon-datasheet/)

## 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.

<svg id="SvgjsSvg1050" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs" width="288" height="154"><defs id="SvgjsDefs1051"><radialGradient id="SvgjsRadialGradient1055" r="0.6"><stop id="SvgjsStop1056" stop-opacity="1" stop-color="blue" offset="0"></stop><stop id="SvgjsStop1057" stop-opacity="1" stop-color="blue" offset="0.3"></stop><stop id="SvgjsStop1058" stop-opacity="0" stop-color="#000000" offset="1"></stop></radialGradient></defs><g id="SvgjsG1052" transform="matrix(2,0,0,2,0.5,0.5)"><image id="SvgjsImage1053" xlink:href="https://docs.particle.io/assets/images/photon-horizontal.svg" width="144" height="77" transform="matrix(1,0,0,1,0,0)"></image><circle id="SvgjsCircle1054" r="15" cx="93.5" cy="38.5" fill="url(#SvgjsRadialGradient1055)"></circle></g></svg>

Next thing you need to do is go to [setup.particle.io](setup.particle.io)

![Setup Particle board](https://res.cloudinary.com/alvarosaburido/image/upload/v1587644605/blog/Particle%20Photon%20101%20Starting%20guide/Setup_Particle_Photon_fpicij.png)

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](https://res.cloudinary.com/alvarosaburido/image/upload/v1587644605/blog/Particle%20Photon%20101%20Starting%20guide/Setup_with_local_file_f35em5.png)

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](https://res.cloudinary.com/alvarosaburido/image/upload/v1587644604/blog/Particle%20Photon%20101%20Starting%20guide/Connect_to_Photon_Wifi_Hotspot_cddjtt.png)

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](https://media.giphy.com/media/5fQyd7jM58m5y/source.gif)

## 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](https://console.particle.io/)

![Your Particle Console](https://res.cloudinary.com/alvarosaburido/image/upload/v1587644604/blog/Particle%20Photon%20101%20Starting%20guide/console.particle.io_c720on.png)

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](https://res.cloudinary.com/alvarosaburido/image/upload/v1587976954/blog/Particle%20Photon%20101%20Starting%20guide/Console%20Particle.png)

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](https://res.cloudinary.com/alvarosaburido/image/upload/v1587978675/blog/Particle%20Photon%20101%20Starting%20guide/particle_101_diagram_z6y5h7.png)

- 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](https://build.particle.io/)
2. Locally using [Particle Workbench](https://docs.particle.io/quickstart/workbench/) or the Workbench Plugin for Visual Studio (it's pretty much the same thing)

In this tutorial, we will use the [Particle Build](https://docs.particle.io/tutorials/developer-tools/build/#web-ide) Web IDE, but in further articles, I will explain how to use the second one.

So enter [https://build.particle.io/](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:

```arduino
void setup() {
  pinMode(led,OUTPUT);
}
```

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

```arduino
void loop() {
   digitalWrite(led,HIGH);
   delay(500);
}
```

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):

```arduino
int led = D0;
int photoresistor = A0;
int power = A5;
int analogvalue;
```

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:

```arduino
void setup() {
  pinMode(led,OUTPUT);
  pinMode(photoresistor,INPUT);
  pinMode(power,OUTPUT);
  ...
}
```

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`

```arduino
void loop() {
    analogvalue = analogRead(photoresistor);
}
```

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):

```arduino
void loop() {
    analogvalue = analogRead(photoresistor);
    voltage = analogvalue * 5.0 / 4095;
}
```

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

![Particle Flash](https://res.cloudinary.com/alvarosaburido/image/upload/v1587644605/blog/Particle%20Photon%20101%20Starting%20guide/Particle_Web_IDE_gtochs.png)

## 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:

```arduino
void setup() {
   ...
  Particle.function("led",ledToggle);
}
```

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:

```arduino
int ledToggle(String command) {

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

}
```

Depending on the command we write in the [console](https://console.particle.io/) we choose to ON/OFF the led. Let's flash the sketch to our device and go to the console.

![Console  function](https://res.cloudinary.com/alvarosaburido/image/upload/v1588098106/blog/Particle%20Photon%20101%20Starting%20guide/Call_function_ck2d7n.png)

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

![It's Alive](https://media.giphy.com/media/11RwocOdukxqN2/source.gif)

## 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](https://docs.particle.io/reference/device-os/firmware/photon/#particle-variable-)

```arduino
float voltage
void setup() {
  ...
  Particle.variable("analogvalue", &analogvalue, INT);
  Particle.variable("voltage", voltage);
  ...
}
```

Flash it into your device and refresh your [console](https://console.particle.io/), 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](https://res.cloudinary.com/alvarosaburido/image/upload/v1588141329/blog/Particle%20Photon%20101%20Starting%20guide/particle_cloud_variables_zxbdkm.png)

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!