Skip to content

Internet of Things Kit Tutorial With Cayenne

In this tutorial, we will use Cayenne to capture some accelerometer readings on their nice Internet of Things dashboard that can be accessed from computer or mobile application!

This tutorial builds on a couple of things, so you may want to check out the TinyZero Setup Tutorial, the TinyZero with Accelerometer BMA250 Tutorial, and the WiFi TinyShield Connection Tutorial before getting started with this project.




Hardware Assembly

You will build this setup by connecting the WiFi shield to the TinyZero board with the 32-pin connectors. You can snap them together like legos, it's that easy.

Software Setup

This part may be a little involved, but it'll be broken down into a lot of small steps. Create a Cayenne account (or use a previous one).

Create a device:

Click in the top left: Add new.../Device/Widget and then you should be opened to this screen where you can select Arduino:

Make the selections Arduino Pro / Wifi101 Shield. You should see your Cayenne mqtt username, password, and client id on this page:

In order to move on from this screen, the board must connect to Cayenne via your WiFi and Cayenne authentication info, so we will move on to the Arduino IDE to prepare for the program by downloading some libraries:

Go to Sketch/Include Library/Manage Libraries... and type in WiFi101 in the window that pops up, click Install on the library called WiFi101:

You'll use the same process to Install the library CayenneMQTT:

Upload Program

Now you can download and program your TinyZero!

Plug in your USB to your TinyZero and make sure your Tools tab choices are correct! Make the following Tools selections to connect to your TinyDuino:

  • Board -> TinyZero
  • Port -> COM## (If you're not sure which Port your Arduino is connected to, check this out for Windows/Mac help)
  • Programmer: "Arduino as ISP"

So now we can download or create files for the program below. When downloading these files or copying and pasting them into new files, make sure the folder they're in has the same name as the .ino program so that everything compiles correctly in Arduino. Often, error windows will pop up immediately to let you know you need to fix this.

 * BMA250 TinyZero Accelerometer Internet of Things Project: 
 * This program uses the TinyZero with an accelerometer populated on it to 
 * connect to WiFi for a Cayenne Internet of Things application
 * Hardware by: TinyCircuits
 * BMA250 Library by: TinyCircuits
 * Code by: Laverena Wienclaw for TinyCircuits

#include <Wire.h>         // For I2C communication with sensor
#include <WiFi101.h>      // For connecting to WiFi
#include "BMA250.h"       // For interfacing with the accel. sensor
#include <CayenneMQTTWiFi101.h> // Cayenne library to connect to transmit data 

/*********************** EDIT THIS SECTION TO MATCH YOUR INFO *************************/
// WiFi network information
char ssid[] = "TinyCircuits";        //  your network SSID (name)
char wifiPassword[] = "__________";  // your network password

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "________________________________________";
char password[] = "________________________________________";
char clientID[] = "________________________________________";

// Accelerometer sensor variables for the sensor and its values
BMA250 accel_sensor;
int x, y, z;
double temp;

void setup() {

  while (!SerialUSB); // ***Will halt program until the Serial Monitor is opened***

  // Connect to WiFi and Cayenne
  Cayenne.begin(username, password, clientID);

  // Set up the BMA250 acccelerometer sensor
  SerialUSB.print("Initializing BMA..."); SerialUSB.println();
  accel_sensor.begin(BMA250_range_2g, BMA250_update_time_64ms); 

void loop() {
  // Take sensor reading and print to Serial Monitor; //This function gets new data from the acccelerometer

  // Get the acceleration values from the sensor and store them into global variables
  // (Makes reading the rest of the program easier)
  x = accel_sensor.X;
  y = accel_sensor.Y;
  z = accel_sensor.Z;
  temp = ((accel_sensor.rawTemp * 0.5) + 24.0);

  // Call function to display sensor readings on Serial Monitor

  // Report sensor reading to Cayenne
  delay(2000); // delay before looping again to take new reading

// Prints the sensor values to the Serial Monitor (found under 'Tools')
void showSerial() {
  SerialUSB.print("X = ");

  SerialUSB.print("  Y = ");

  SerialUSB.print("  Z = ");

  SerialUSB.print("  Temperature(C) = ");

// This function goes through the steps to connect to WiFi using the WiFi101 library
void connectWifi(void){
  // Connect to Wifi network:
  SerialUSB.print("Connecting Wifi: ");
  WiFi.begin(ssid, wifiPassword);

  // Loop to tell user that WiFi connection was not achieved
  while (WiFi.status() != WL_CONNECTED)
    SerialUSB.print("Connection failed: Move closer to router, or double check network info.");

  // Success message and put WiFi in low power mode to save energy
  SerialUSB.println("WiFi connected");

// Default function for sending sensor data at intervals to Cayenne.
  // Write data to Cayenne here:
  Cayenne.virtualWrite(1, x);
  Cayenne.virtualWrite(2, y);
  Cayenne.virtualWrite(3, z);
  Cayenne.virtualWrite(4, temp);

You’ll need to change:

  • The ssid[] variable, make this your WiFi's name
  • The wifiPassword[] variable, make it your WiFi password
  • The username[], password[], and clientID[] variables generated from Cayenne

Once you make those changes and download the program, you must open up the Serial Monitor in the Arduino IDE under Tools/Serial Monitor. If you do not open the Serial Monitor, the program won't move forward from the line that says "while(!SerialUSB);" so the WiFi won't connect until you do this step.

The Serial Monitor will print out the values from the BMA250 accelerometer sensor so you can see them before they are sent to Cayenne.

Speaking of Cayenne, once the WiFi connects with your authentication info, the Cayenne screen should find your device and open up to your dashboard:

At the end of the program, a function called CAYENNE_OUT_DEFAULT() will be sending the x, y, z, and temperature data read from the BMA250 sensor to Cayenne. This data should show up after a minute or two and look something like this:

You should see a '+' button to add those channels to your dashboard permanently.

Now that you have data coming from the BMA250 sensor, you can re-label and make the data look a little nicer!

You can click on the graph icon on any of the data blocks to see a graph of data readings over time.

Customize your dashboard however you like to use your new Internet-Connected device! Cayenne even has an app you can use to monitor your data readings on the go.

Contact Us

If you have any questions or feedback, feel free to email us or make a post on our forum. Show us what you make by tagging @TinyCircuits on Instagram, Twitter, or Facebook so we can feature it.

Thanks for making with us!