Particle Photon Tutorial: Unleash the Power of Particle in IoT
Unleash the power of Particle Photon in this IoT tutorial.
Join the DZone community and get the full member experience.
Join For FreeThis Photon Particle tutorial covers how to unleash the power of Particle Photon starting from the ground to integrating Photon with the cloud.
As you may already know, Photon Particle is an interesting IoT device that can be used in several scenarios. This Photon Particle tutorial describes several use cases where we can use this IoT device. During this Photon tutorial, you will learn:
- How to get started using Photon
- How to build your first Photon project integrating it with BMP280
- How to connect Photon to the cloud and get the temperature and pressure
- How to use Particle Photon with Neopixel LEDs and control it
What Is a Photon Particle?
Before starting this Particle Photon tutorial, it is useful to describe briefly what is Photon Particle and its main features.
Particle Photon is a small IoT device with Wi-Fi built-in that can be used to build IoT projects. Particle Photon is part of the Particle ecosystem that offers an integrated environment to build and deploy IoT projects. Moreover, it supports cloud connectivity so that we can control Particle Photon from the cloud. From the hardware point of view, Photon has a Cypress Wi-Fi chip and STM32 ARM Cortex M3 microcontroller. As we can see later, Particle offers a Web IDE to develop IoT projects and a desktop IDE.
Getting Started Using Particle Photon
When we get for the first time the Photon, before using it, it is necessary to configure it and connect it to the Wi-Fi to unleash the power of the Photon.
Follow these steps:
- Go to setup.particle.io
- Download the file HTML
- Open the file
Once you opened the file, connect to the Photon Wi-Fi:

Configure the Wi-Fi credential to connect to your Wi-Fi:

Finally, you can configure the name of your Photon:

That’s all. Your device is ready:

How to Build Your First Photon Project and Integrate it With BMP280
Once the Particle Photon is configured, we can develop the first project of this Particle Photon tutorial. There are two different options to start developing an IoT project with Photon:
- Using Web IDE
- Using Desktop IDE
It is up to you to choose the one you prefer. In this Particle Photon tutorial, we use the Desktop IDE any way you can do the same things using the Web.
This first project uses a temperature and humidity sensor (BMP280). Let us connect the BMP280 to the Photon; the picture below describes how to do it:

This sensor is an I2C sensor, so we need four different connections;
- Vcc (+3.3V)
- GND
- CLK
- SDA
Open your IDE and start coding. Before using the sensor, it is necessary to import the library that handles this sensor. You can do it using the Library Manager and looking for the sensor, as shown in the picture below:

Then, add the library to your project. That’s all; we are ready to use the sensor.
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp280;
double temp;
double press;
void setup() {
Serial.begin(9600);
if (!bmp280.begin()) {
Serial.println("Can't find the sensor BMP280");
}
Serial.println("BMP280 connected!");
}
void loop() {
temp = bmp280.readTemperature();
press = bmp280.readPressure();
Serial.println("Temperature ["+String(temp)+"] - Pressure ["+String(press)+"]");
delay(1000);
}
This simple Photon code reads the temperature and the pressure detected by the BMP280. Click on the flash icon and wait until the firmware is flashed.
Open the serial console and check the current temperature and pressure.
How to Connect Photon to the Cloud and Get the Temperature and Pressure Using Particle.variable
It is time to connect the Photon to the cloud and get the current temperature and pressure. Particle Photon has an interesting feature that simplifies the cloud connection. As you remember, we have covered how to connect Arduino to the cloud using the API library. Well, we can do the same in a really simple way.
In this Photon tutorial, we want to access to the temperature and the pressure from the cloud. To do it, let us modify the code shown above in this way:
void setup() {
Serial.begin(9600);
Particle.variable("temp", &temp, DOUBLE);
Particle.variable("press", &press, DOUBLE);
if (!bmp280.begin()) {
Serial.println("Can't find the sensor BMP280");
}
Serial.println("BMP280 connected!");
}
To publish a variable to the cloud, it is necessary to use:
Particle(variable name, reference to the variable, variable type)
In the example above, the temperature is published as temp
, and the pressure is published as press
.
Now, it is possible to read, from the cloud, the variable values using a browser. Before doing it, it is necessary to have the device ID and the authorization token. To this purpose, go to the console and you should see your connected device:

To retrieve the authorization token, go to the web console and then to the settings:

Now, we can call retrieve the temperature using:
How to Use Particle Photon With Neopixel LEDs and Control It
The next IoT project covered in this Particle Photon tutorial is how to control Neopixel LEDs and the Particle. You can use several Neopixel LEDs. In this Particle tutorial, we are using the Neopixel Ring. Anyway, if you use a different type of Neopixel, the connections remain the same. The picture below shows how to connect Neopixel to Photon:

The code to handle these LEDs is simple:
#include "neopixel/neopixel.h"
#define PIXEL_COUNT 24
#define PIXEL_PIN D6
#define PIXEL_TYPE WS2812B
// Init the LED strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
void setup() {
strip.begin();
}
void loop() {
// The core of your code will likely live here.
for (int i=0; i < PIXEL_COUNT; i++)
strip.setColor(i, 255,0,0);
strip.show();
}
In this example, the Photon turns on all the Neopixel LEDs using red color. Before using this code, it is necessary to import the Neopixel library into your project.
How to Use Particle.function to Publish Function to the Cloud
Once we have connected the LEDs, we can control them remotely from the cloud. The way is almost the same way we have covered it in the previous paragraph, and we have to use Particle.function
. Let us modify the previous code:
#include "neopixel/neopixel.h"
#define PIXEL_COUNT 24
#define PIXEL_PIN D6
#define PIXEL_TYPE WS2812B
// Init the LED strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
int green;
int red;
int blue;
void setup() {
Particle.function("red", setRed);
Particle.function("green", setGreen);
Particle.function("blue", setBlue);
Serial.begin(9600);
strip.begin();
}
void loop() {
}
void setStripColor() {
Serial.println("Set color ["+String(red)+"," +String(green)+ "," +String(blue)+ "]");
for (int i=0; i < PIXEL_COUNT; i++)
strip.setColor(i, red, green, blue);
strip.show();
}
int setRed(String r) {
red = r.toInt();
setStripColor();
return red;
}
int setGreen(String g) {
green = g.toInt();
setStripColor();
return green;
}
int setBlue(String b) {
blue = b.toInt();
setStripColor();
return blue;
}
Notice that we added three different methods to handle the three different colors and exposed these methods using Particle.funcion. Now, you can remotely control the LEDs from the cloud.
For additional resources, check out the following:
Build an IoT soil moisture monitor using Arduino with an IFTTT alert system
How to use Cayenne IoT with ESP8266 and MQTT: Complete Step-by-step practical guide
How to Use MQTT to Publish Data From Photon Using Particle.publish
In this last project, we wanted to publish the data acquired from the sensor using MQTT and Particle.publish
. To do it, we will reuse the source code that reads data from BMP280, and we want to publish these values to the cloud. The code is shown below:
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp280;
double temp;
double press;
void setup() {
Serial.begin(9600);
Particle.variable("temp", &temp, DOUBLE);
Particle.variable("press", &press, DOUBLE);
if (!bmp280.begin()) {
Serial.println("Can't find the sensor BMP280");
}
Serial.println("BMP280 connected!");
}
void loop() {
temp = bmp280.readTemperature();
press = bmp280.readPressure();
Particle.publish("temperature", String(temp), 60, PRIVATE);
Particle.publish("pressure", String(press), 60, PRIVATE);
delay(1000);
}
In the loop()
method, the Photon code publishes two events called:
- Temperature
- Pressure
The first one is the value related to the temp
variable, while the pressure is related to the press
value. You can check the value published using the event console:

Final Considerations
At the end of this post, hopefully, you gained knowledge about how to use Particle Photon in different scenarios. You had the chance to verify its power and how simply it is to build an IoT system.
Published at DZone with permission of Francesco Azzola, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments