Basic Sketch

Table of contents

  1. Create Global Variables
  2. Configuring our board
  3. Reading the sensor
  4. Flash it

Open Particle Dev or Particle Build and create a new project. Give it a memorable name and start the process of writing your code:

You should see an initial template like

void setup(){

void loop(){

We’ll add to these in a moment

Create Global Variables

The first step in any sketch is to add a series of global variables that store useful bits of information - like the pins we’ll want to use, or sensor values we might want to store.

Add the following to the top of your code file i.e. before void setup()

// variables
int ledPin = D0;  // LED Pin mapping

int photoPin = A0; // Photocell Pin Mapping 

int photoReading = 0; // Reading from the Photocell

bool isBoxOpen = false; // A value to store if the box is open or closed

Once added, save your file!

Configuring our board

Now in the setup(), we’ll add some code to configure how we use the pins on our board, and to blink the LED to signal the board as completed the setup.

Add some code to setup so it looks like this:

void setup()

  pinMode(ledPin, OUTPUT);

  for( int i = 0; i < 3; i++ ){
    digitalWrite(ledPin, HIGH);
    digitalWrite(ledPin, LOW);


Reading the sensor

In our loop, we’ll want to

  • Regularly read the value from the photo cell to see if it’s bright or dim
  • Use this value to decide if the box is open or closed.

```` void loop() { // read a new value from the photo cell photoReading = analogRead(photoPin);

delay( 100 ); // pause 1/10th of a second


The code above will do the basics of reading from the photo cell

Flash it

Now that you’ve got the basics of the code written. Let’s test it out.

  1. Choose a device to flash to and press the lightning bolt button
  2. In a couple of seconds you should see your device turn magenta
  3. After a couple more seconds, it’ll reboot and connect to WiFi. This means your code is loaded onto the microcontroller.

You should see the LED blink.

Great. Next, let’s connect it to the internet!

Table of contents