---
title: Sensor Tutorial with C/C++ (Arduino/Wiring) and Adafruit HUZZAH32
description: Build an MCU-controlled application that reads from an external sensor and sends readings to the Notecard to start building your IoT application using Cellular, Satellite, LoRa, or WiFi connectivity.
source_url: https://dev.blues.io/guides-and-tutorials/collecting-sensor-data/adafruit-huzzah32/c-cpp-arduino-wiring/
canonical_url: https://dev.blues.io/guides-and-tutorials/collecting-sensor-data/adafruit-huzzah32/c-cpp-arduino-wiring/
markdown_url: https://dev.blues.io/guides-and-tutorials/collecting-sensor-data/adafruit-huzzah32/c-cpp-arduino-wiring.md
---

# Collecting Sensor Data C/C++ (Arduino/Wiring) and Adafruit HUZZAH32

*Don't see your favorite hardware here? Rest assured the Notecard works with virtually every MCU and SBC available. If you can't figure out how to complete this tutorial [let us know in our forum](https://discuss.blues.com/) and we can help you out.*

## Introduction

*This tutorial should take approximately 40-50 minutes to complete.*

In this tutorial, you'll learn how to take sensor data, queue it on your Notecard, and synchronize it to Blues Notehub. You'll use **C/C++ (Arduino/Wiring)** running on a **Adafruit HUZZAH32**. If you would like to use a different language or board, modify the dropdowns at the top of this guide. And if you get stuck at any point, feel free to reach out for help in our [community forum](https://discuss.blues.com).

This tutorial uses mock sensor readings for simplicity, but feel free to hook up a physical sensor of your choice and use that instead. The goal of this tutorial is to demonstrate reusable firmware techniques you can apply to your own Notecard-based projects.

## Set up Hardware

> **Note:**
>
> This tutorial assumes you have already wired your Notecard to your host. If you haven't, complete the [Host Wiring Guide](https://dev.blues.io/guides-and-tutorials/host-wiring-guide.md) before continuing.

In order to complete this guide, you'll need the following:

- Any ESP32-compatible Microcontroller (MCU). *We'll be using the [Adafruit HUZZAH32](https://www.adafruit.com/product/3405), but any MCU that can be programmed with the Arduino IDE will do.*

* Micro USB to USB-A cable.

> **Note:**
>
> For this tutorial, you'll be powering the Notecard through the Micro USB connection of your Adafruit HUZZAH32. Some Feather-compatible devices cannot handle 2 Amp pulses from the Notecard when connected to GSM, so if you experience resets or other power-related issues, we suggest powering your Notecard separately through the USB or LiPo connector on your Notecarrier.

## Create a Notehub Project

Now that your hardware is all connected, let's create a new Notehub project to receive sensor readings from your Notecard.

1. Navigate to [notehub.io](https://notehub.io) and log in, or create a new account.

2. Click the **Create Project** button.

3. In the New Project dialog, give your project a name and ProductUID.

   ![How to create a new Notehub project](https://dev.blues.io/images/guides/notehub/create-project.png?v=bf29a47a)

   > **Note:**
   >
   > The ProductUID must be globally unique, so we recommend a namespaced name like `"com.your-company.your-name:your_product"`.

4. Take note of your ProductUID. This identifier is used by Notehub to associate your Notecard with your project.

   ![Where to find your product UID](https://dev.blues.io/images/guides/notehub/product-uid.png?v=d829ab1d)

## Write Firmware

> **Tip:**
>
> This tutorial shows how to write host firmware by hand so you can understand the fundamentals. In practice, we recommend using an AI coding assistant with Blues Expert MCP for development. Blues Expert MCP connects your AI coding assistant (Claude Code, GitHub Copilot, Cursor) directly to our API docs, providing live request validation and firmware best practices for Arduino, C, Zephyr, and Python. [Install the Blues Expert MCP →](https://dev.blues.io/tools-and-sdks/generative-ai-tools/blues-expert-mcp.md)

### Configure the Arduino IDE

For this portion of the guide, we'll be using the Arduino IDE, so be sure to [install version 2.0+](https://www.arduino.cc/en/software) if you haven't already.

Once installed, we'll need to add support for your Adafruit HUZZAH32 Board.

### Configure the Arduino Boards Manager to use the ESP32

1. Start the Arduino IDE and open the **Preferences** menu.

   ![Arduino Preferences menu option](https://dev.blues.io/images/guides/first-sensor/feather/prefs-menu.png?v=b932e3db)

2. Copy the following path `https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json` into the "Additional Board Manager URLs" field. If there's already something in the box, then add a comma to separate the URLs.

   ![Additional Board Manager URLs field](https://dev.blues.io/images/guides/first-sensor/feather/arduino-preferences.png?v=23ed2ca1)

3. Click OK, then open the Boards Manager from the **Tools > Board: \[board name] > Boards Manager...** menu.

   ![Boards Manager menu option](https://dev.blues.io/images/guides/first-sensor/feather/boards-mgr-menu.png?v=ac5317d9)

4. Search for "esp32" and click the Install button to add ESP32 board support to the Arduino IDE.

   ![ESP32 board support install](https://dev.blues.io/images/guides/first-sensor/feather/boards-mgr.png?v=587bb9b8)

5. Once the installation is complete, click the **Close** button.

6. To proceed, you may need to install the SiLabs CP2104 driver. This driver allows the Arduino IDE to communicate with (and flash) your ESP32 DevKit over USB.

   - **macOS Big Sur (and later):** The necessary driver is already installed.
   - **Earlier versions of macOS:** Download and install "CP210x VCP Mac OSX Driver" from [SiLabs](https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers).
   - **Windows 10:** Download and install "CP210x Universal Windows Driver" from [SiLabs](https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers).
   - **Windows 7 to 8.1:** Download and install the "CP210x Windows Drivers" from [SiLabs](https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers).
   - **Linux:** Download and install the appropriate CP210x driver for your Linux kernel from [SiLabs](https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers).

   *Linux note: If your kernel version is >= 5, you do not need to install the driver. If you are unsure, you can check your kernel version by running `uname -srm` from your terminal application.*

   ![CP210x VCP driver installer](https://dev.blues.io/images/guides/first-sensor/feather/si-labs-driver.png?v=eb73ec21)

7. Next, plug your Arduino device back in, open the Arduino IDE, select **Adafruit ESP32 Feather** from the **Tools > Board** menu, and select the appropriate Port for your device.

   ![Selecting device serial port](https://dev.blues.io/images/guides/first-sensor/feather/select-port.png?v=a2991cf4)

**Additional Linux Setup**

In order to support the Adafruit HUZZAH32, you will need to install the following packages:

```bash
sudo apt update && sudo apt install \
  python-is-python3 \
  python3-serial
```

> **Warning:**
>
> The Arduino IDE MUST NOT be installed from the Ubuntu store. Instead, it must be installed from the ZIP file downloaded from the Arduino [downloads page](https://www.arduino.cc/en/Main/Software).

### Communicating With Your Notecard

When communicating with the Notecard, the easiest approach is to use one of [Notecard's official SDKs](https://dev.blues.io/tools-and-sdks/firmware-libraries.md). Since this tutorial uses Arduino, let's install the Arduino SDK, [note-arduino](https://github.com/blues/note-arduino).

**Install the Notecard Arduino Library**

1. To use the [note-arduino library](https://github.com/blues/note-arduino), you'll need to add it to the Arduino IDE.

2. Click on **Tools > Manage Libraries...**

3. Search for "Blues" in the input box and click the "Install" button next to the "Blues Wireless Notecard" result.

   ![Installing the Blues Wireless Notecard library.](https://dev.blues.io/images/guides/first-sensor/arduino/install-library.png?v=ea1aec79)

4. Create a new sketch and select the **Sketch > Include Library > Contributed Libraries > Blues Wireless Notecard** menu option, to add the following include to your sketch:

   ```cpp
   #include <Notecard.h>
   ```

**Set Up Your Notecard**

Select the tab matching the interface you wired up in the [Host Wiring Guide](https://dev.blues.io/guides-and-tutorials/host-wiring-guide.md), then follow the steps for your chosen interface.

> **Note:**
>
> If you're using a Notecarrier CX or Notecarrier F, you're likely using I2C, as those boards connect the host to Notecard over I2C by default.

**I2C**

1. At the top of your sketch, define the Serial interface you'll use for logging.

   ```cpp
   #define usbSerial Serial
   ```

2. Add a definition for your ProductUID using the value you specified when creating your Notehub project.

   ```cpp
   #define productUID "com.your-company.your-name:your_product"
   ```

3. Above the `setup()` and `loop()` functions, declare a **global** object to represent the Notecard.

   ```cpp
   Notecard notecard;
   ```

4. In the `setup()` function, initialize the `usbSerial` object. The short delay gives boards with native USB time to enumerate before the first log line is written.

   ```cpp
   delay(2500);
   usbSerial.begin(115200);
   ```

5. Initialize the I2C connection to the Notecard using the `notecard.begin()` function. Then, use `setDebugOutputStream()` to link the debug output to `usbSerial` with the following code:

   ```cpp
   notecard.begin();
   notecard.setDebugOutputStream(usbSerial);
   ```

**UART**

1. At the top of your sketch, define the Serial interfaces you'll use for logging and Notecard communication.

   ```cpp
   #define usbSerial Serial
   #define txRxPinsSerial Serial1
   ```

2. Add a definition for your ProductUID using the value you specified when creating your Notehub project.

   ```cpp
   #define productUID "com.your-company.your-name:your_product"
   ```

3. Above the `setup()` and `loop()` functions, declare a **global** object to represent the Notecard.

   ```cpp
   Notecard notecard;
   ```

4. In the `setup()` function, initialize the `usbSerial` object.

   ```cpp
   usbSerial.begin(115200);
   while (!usbSerial) {
       ; // wait for serial port to connect. Needed for native USB
   }
   usbSerial.println("Starting...");
   ```

5. Initialize the UART connection to the Notecard using the `begin()` function. Then, use `setDebugOutputStream()` to link the debug output to `usbSerial` with the following code:

   ```cpp
   notecard.begin(txRxPinsSerial, 9600);
   notecard.setDebugOutputStream(usbSerial);
   ```

6) Now, we'll configure the Notecard. Using the `hub.set` request, we associate this Notecard with the ProductUID of your project and set the Notecard to operate in `continuous` mode, which indicates that the device should immediately make a connection to Notehub and keep it active.

   ```cpp
   {
     J *req = notecard.newRequest("hub.set");
     if (req != NULL) {
       JAddStringToObject(req, "product", productUID);
       JAddStringToObject(req, "mode", "continuous");
       notecard.sendRequest(req);
     }
   }
   ```

   The lines above build up a JSON object by adding two string values for product and mode, and then fire the request off to the Notecard with the `sendRequest()` function.

**Flash Your Firmware**

1. Click the Upload button (right arrow icon) to flash firmware to your device.

   ![Arduino IDE Upload button](https://dev.blues.io/images/guides/first-sensor/feather/arduino-tutorial-upload.png?v=cc42f1f4)

**View the Serial Log**

Now that your firmware is running on your device, you can use a Serial Monitor to view device output. If you are using an STLINK programmer, you will need to connect a second USB cable from your computer directly to your STM32-based host to view serial output.

Complete the instructions below in your preferred IDE.

1. Open the Arduino Serial Monitor. If everything has been connected and configured properly, you'll see a few debug messages, including the JSON object you sent, as well as the response from the Notecard `{}`.

   ![Serial Monitor hub.set response](https://dev.blues.io/images/guides/first-sensor/feather/serial-configured.png?v=38df04b5)

### Read from the sensor

Now that you've configured your Arduino to communicate with the Notecard, let's grab some sensor readings.

> **Note:**
>
> If you have your own sensor, feel free to hook it up and use your own values instead of this tutorial's mocked ones.

**Install the NotecardPseudoSensor Library**

1. To generate mock sensor readings you'll use the [NotecardPseudoSensor library](https://github.com/blues/notecard-pseudo-sensor). To add the library to Arduino IDE start by clicking on **Tools > Manage Libraries...**

2. Search for "NotecardPseudoSensor" in the input box and click the **Install** button next to the "Blues Wireless Notecard Pseudo Sensor" result.

   ![NotecardPseudoSensor library install](https://dev.blues.io/images/guides/first-sensor/arduino/pseudo-sensor-library.png?v=929defbe)

**Gather Sensor Readings**

1. Add the following include to the top of your sketch:

   ```cpp
   #include <NotecardPseudoSensor.h>
   ```

2. Next, include the following `namespace` under your includes.

   ```cpp
   using namespace blues;
   ```

3. After that, create an instance of `NotecardPseudoSensor` with the line of code below. Place this directly under your existing `Notecard notecard` statement.

   ```cpp
   NotecardPseudoSensor sensor(notecard);
   ```

4. Finally, place the following code in your `loop` function, which generates mock temperature and humidity readings, prints them to the console, and then waits 15 seconds before exiting the loop.

   ```cpp
   float temperature = sensor.temp();
   float humidity = sensor.humidity();

   usbSerial.print("Temperature = ");
   usbSerial.print(temperature);
   usbSerial.println(" *C");
   usbSerial.print("Humidity = ");
   usbSerial.print(humidity);
   usbSerial.println(" %");

   delay(15000);
   ```

5. Upload this code to your Adafruit HUZZAH32. Open the Serial Monitor and you'll see temperature and humidity readings every 15 seconds.

   ![Serial monitor temperature and humidity](https://dev.blues.io/images/guides/first-sensor/arduino/arduino-tutorial-serial-monitor.png?v=09c9c2c5)

### Send Sensor Readings to the Notecard

Now that we're getting sensor readings, let's send these to our Notecard.

1. To send a sensor reading to the Notecard, we'll need to construct a new JSON request to the `note.add` API that includes a new Notefile name (`sensors.qo`), sets the `sync` field to true to instruct the Notecard to sync to Notehub immediately, and finally, sets the `body` to the sensor temperature and humidity. Add the following in `loop` right after the `usbSerial` commands to print out readings.

   ```cpp
   {
     J *req = notecard.newRequest("note.add");
     if (req != NULL) {
       JAddStringToObject(req, "file", "sensors.qo");
       JAddBoolToObject(req, "sync", true);
       J *body = JAddObjectToObject(req, "body");
       if (body) {
         JAddNumberToObject(body, "temp", temperature);
         JAddNumberToObject(body, "humidity", humidity);
       }
       notecard.sendRequest(req);
     }
   }
   ```

2) Upload this code to your device. After reboot, the Serial monitor will update to display the response from the `note.add` request (the total number of Notes in the notefile) each time you add a new reading.

   ![Serial Monitor note totals output](https://dev.blues.io/images/guides/first-sensor/feather/notes-mock-lib.png?v=5ec543e1)

> **Note:**
>
> If you're using a Notecard for LoRa to complete this tutorial you have one additional step. Because the Notecard for LoRa requires a template for every Notefile you use, you must define a template for the `sensors.qo` Notefile in your `setup()` function. You can add the following code to the bottom of your `setup()` function to fix the problem.
>
> ```cpp
> {
>   J *req = notecard.newRequest("note.template");
>   if (req != NULL) {
>     JAddStringToObject(req, "file", "sensors.qo");
>     JAddNumberToObject(req, "port", 1);
>     JAddStringToObject(req, "format", "compact");
>     J *body = JCreateObject();
>     if (body != NULL) {
>       JAddNumberToObject(body, "temp", 14.1);
>       JAddNumberToObject(body, "humidity", 14.1);
>       JAddItemToObject(req, "body", body);
>       notecard.sendRequest(req);
>     }
>   }
> }
> ```
>
> Learn more about Notefile templates in [Working with Note Templates](https://dev.blues.io/notecard/notecard-walkthrough/low-bandwidth-design.md#working-with-note-templates).

> **Note:**
>
> Concerned about the size of `note-arduino`? You can [communicate with the Notecard without using the library](https://dev.blues.io/guides-and-tutorials/notecard-guides/notecard-communication-without-a-library.md).

## View Data in Notehub

Once you start capturing readings, your Notecard will initiate a connection to Notehub and will start transferring Notes. Depending on signal strength and coverage in your area, it may take a few minutes for your Notecard to connect to Notehub and transfer data.

1. Return to [notehub.io](https://notehub.io) and open your project. You should see your Notecard in the Devices view.

   ![The new device in Notehub](https://dev.blues.io/images/guides/notehub/new-device.png?v=f506b425)

   > **Note:**
   >
   > Each Notecard has a factory-assigned, globally unique identifier known as a [DeviceUID](https://dev.blues.io/api-reference/glossary.md#deviceuid). Notehub uses this identifier in the Devices view by default (for example, `dev:868531061604976` in the screenshot above).
   >
   > If you’d prefer to use your own identifier—such as a human-readable name or an internal ID—you can assign a [serial number](https://dev.blues.io/api-reference/glossary.md#product-sn) to your Notecard in one of the following ways:
   >
   > - **In Notehub:** Double-click your device in the Devices view to open its details, where you can edit the serial number.
   > - **Via the Notehub API:** Set the reserved `_sn` environment variable using the [Set Device Environment Variables](https://dev.blues.io/api-reference/notehub-api/device-api.md#set-device-environment-variables) endpoint.
   > - **Via the Notecard API**: Include an `sn` argument in the [hub.set request](https://dev.blues.io/api-reference/notecard-api/hub-requests/latest.md#hub-set) you used to configure your Notecard.

2. Now, click on the Events left menu item. Once your sensor Notes start syncing, they'll show up here. You may need to refresh the page to see newly synced Notes.

   ![The event list in Notehub](https://dev.blues.io/images/guides/notehub/events.png?v=76323245)

## Use Environment Variables

[Environment variables](https://dev.blues.io/guides-and-tutorials/notecard-guides/understanding-environment-variables.md) are a Notehub state and settings management feature that allow you to set variables in key-value pairs, and intelligently synchronize those values across devices and fleets of devices.

In this section you'll learn how environment variables work by creating a variable that determines how often your firmware should take sensor readings.

### Using Environment Variables in Firmware

The Notecard provides [a set of requests for working with environment variables](https://dev.blues.io/api-reference/notecard-api/env-requests.md). The most common of these requests is [`env.get`](https://dev.blues.io/api-reference/notecard-api/env-requests.md#env-get), which allows you to retrieve the value of an environment variable.

Complete the steps below to use the `env.get` request to retrieve and use the `reading_interval` environment variable.

1. In `setup()`, adjust your existing `hub.set` configuration to set the `inbound` argument to `5`. This tells your Notecard to look for inbound changes from Notehub every 5 minutes.

   ```c
   {
     J *req = notecard.newRequest("hub.set");
     if (req != NULL) {
       JAddStringToObject(req, "product", productUID);
       JAddStringToObject(req, "mode", "continuous");
       JAddNumberToObject(req, "inbound", 5); // ADD THIS LINE
       notecard.sendRequest(req);
     }
   }
   ```

2. Next, place the following new function at the bottom of your sketch.

   ```c
   // This function assumes you’ll set the reading_interval environment variable to
   // a positive integer. If the variable is not set, set to 0, or set to an invalid
   // type, this function returns a default value of 60.
   int getSensorInterval() {
     int sensorIntervalSeconds = 60;
     J *req = notecard.newRequest("env.get");
     if (req != NULL) {
       JAddStringToObject(req, "name", "reading_interval");
       J* rsp = notecard.requestAndResponse(req);
       int readingIntervalEnvVar = atoi(JGetString(rsp, "text"));
       if (readingIntervalEnvVar > 0) {
         sensorIntervalSeconds = readingIntervalEnvVar;
       }
       notecard.deleteResponse(rsp);
     }
     return sensorIntervalSeconds;
   }
   ```

3. Then, add this line after the `#include` lines at the top of the file.

   ```c
   int getSensorInterval();
   ```

4. Next, find the `delay(15000)` line at the bottom of your `loop()` function, and replace it with the code below.

   ```c
   int sensorIntervalSeconds = getSensorInterval();
   usbSerial.print("Delaying ");
   usbSerial.print(sensorIntervalSeconds);
   usbSerial.println(" seconds");
   delay(sensorIntervalSeconds * 1000);
   ```

   > **Note:**
   >
   > Notecard for LoRa requires a template for each environment variable you use. If you're using a Notecard for LoRa to complete this tutorial, add the code below to your `setup()` to provide a template for the `reading_interval` variable.
   >
   > ```c
   > {
   >   J *req = notecard.newRequest("env.template");
   >   if (req != NULL) {
   >     J *body = JCreateObject();
   >     if (body != NULL) {
   >       JAddNumberToObject(body, "reading_interval", 21);
   >       JAddItemToObject(req, "body", body);
   >       notecard.sendRequest(req);
   >     }
   >   }
   > }
   > ```
   >
   > Here `21` is the type hint for a 1-byte unsigned integer (`0`–`255`). If your `reading_interval` may exceed 255, use `22` (a 2-byte unsigned integer) instead.

With this code in place, your firmware now uses the `reading_interval` environment variable to determine how many seconds to delay in between sensor readings. If you flash this updated code to your device and open your serial monitor, you can see the device using the default value for `reading_interval` of 60 seconds.

### Setting an Environment Variable

Now that we have our device programmed to retrieve an environment variable from Notehub, we will create that variable. Environment variables can be set in the Notehub UI or through the Notehub API. In this tutorial you'll learn how to set the values through the Notehub UI. If you'd like to instead set environment variables through the Notehub API, refer to environment variable requests in the [Project API](https://dev.blues.io/api-reference/notehub-api/project-api.md).

1. Return to your Notehub project, go to the **Devices** page, and double click your device. You should see a screen that looks like this.

   ![The Notehub device screen](https://dev.blues.io/images/guides/first-sensor/environment/device-screen.png?v=cdb91b6c)

2. Click the **Environment** tab.

3. Under the **Device environment variables** header, define a new environment variable named `reading_interval` and set its value to `30`.

   ![The environment screen with a new value set](https://dev.blues.io/images/guides/first-sensor/environment/setting-a-variable.png?v=e576ef35)

Now that you have an environment variable set, you'll see it reflected on your device **after your configured `inbound` interval has passed**.

![The environment screen with a new value set](https://dev.blues.io/images/guides/first-sensor/environment/env-var-serial-log.png?v=123d9606)

> **Note:**
>
> On cellular and WiFi-based Notecards you can use the [`hub.set` request](https://dev.blues.io/api-reference/notecard-api/hub-requests/latest.md#hub-set)'s `sync` argument to immediately receive inbound updates instead of relying on the `inbound` interval.

And with that, you've used your first environment variable on your Notecard! To see the real power of environment variables in action, try returning to Notehub and updating your device's `reading_interval` with your serial monitor open.

> **Note:**
>
> This tutorial covers several configuration settings that are best used when you have your Notecard connected to mains power.
>
> - In the `hub.set` request, setting `mode` to `"continuous"` tells the Notecard to maintain an active network connection.
>
> - In the `hub.set` request, the `sync` argument (when set to `true`) tells the Notecard to immediately synchronize inbound Notes and environment variables from Notehub.
>
> - In the `note.add` request, setting `sync` to `true` tells the Notecard to immediately synchronize all outbound Notes to Notehub.
>
> Because each of these settings causes the Notecard to use more power, you may wish to disable them if you plan to transition your project to battery power. You can run the command below to put your Notecard into `periodic` mode with the `sync` argument turned off.
>
> ```json
> {
>  "req": "hub.set",
>  "mode": "periodic",
>  "sync": false,
>  "outbound": 60,
>  "inbound": 120
> }
> ```
>
> For a deeper look at how these settings work together, watch [An In-Depth Guide to Notecard’s hub.set Request](https://www.youtube.com/watch?v=3D2p1t8UMHQ). The video steps through complete configuration scenarios that show how your choice of `mode`, `outbound`, `inbound`, and `sync` values determines exactly when your Notecard connects and syncs with Notehub.

## Next Steps

**Congratulations!** You've successfully connected your Adafruit HUZZAH32 to your Notecard and built a basic IoT project.

If you're following a Cell+WiFi Quickstart, next we recommend learning how to send (and visualize) your data in a cloud application:

1. ~~Use the Notecard to Send Data~~
2. ~~Host Wiring Guide~~
3. ~~Build Your First IoT App With Blues~~
4. [Send Data to Your Cloud](https://dev.blues.io/guides-and-tutorials/routing-data-to-cloud.md)

At any time, if you find yourself stuck, please reach out on the [community forum](https://discuss.blues.com/).
