Lab 3: Afero + Arduino Temperature Sensor

This lab will demonstrate how to:

  • Connect a Modulo dev board to a simple microcontroller.
  • Send data from the MCU to the Afero Cloud.
  • Receive data from the Afero Cloud to the MCU.

This lab will take about an hour, depending on your familiarity with the popular Arduino MCU and its development environment. No previous experience with the Arduino MCU is required to complete this lab.

The version of afLib used in this lab is afLib3, written in C. The C++ version of afLib has now been deprecated; however, Lab 4 still is based on this deprecated version.

If you need the Lab 3 that uses the deprecated C++ afLib, download this archive file (.zip).


You should have completed the Lab 2: Afero Profile Editor, and be able to modify a Profile and publish it to an Modulo development board over-the-air.


Before beginning this lab, you will need:

  • The following pieces of hardware:
    • Arduino Uno MCU
    • Afero Plinto interface board to connect your Modulo dev board to the Arduino Uno
    • MCP9700 Temperature Sensor (small component with three wire leads)
    • USB-A to USB-B cable to connect the Arduino to your computer
  • Windows or Macintosh computer
  • iOS or Android smartphone, running:
    • Android KitKat 4.4 (API Level 19), or
    • iOS release 9.3
  • Afero Modulo-1 or Modulo-2 development board
  • Micro-USB cable to power the Modulo dev board
  • USB power source (free USB port on a computer, AC wall adapter, or portable charger)

Arduino IDE Setup

  1. Download the Arduino IDE from the web:
  2. Install the Arduino IDE following their online instructions:
  3. Download the lab project file, Save this file to your computer’s Downloads folder.

    Copy the files in this .zip file to your computer into the specific locations listed below. Please note that it is important to put these folders in the locations specified so the Arduino IDE can properly locate the code needed for the lab.

Arduino IDE Setup for Windows

  1. Open a File Explorer window either by clicking the File Explorer icon in the taskbar or selecting Computer from the Start Menu.
  2. Click the Downloads folder in the left pane, and find the file named ArduinoLab. Downloads folder
  3. Double-click the ArduinoLab compressed folder. In that file you will see two folders, one named libraries and another named AferoMCULab. Arduino Lab Files
  4. In the left pane of the File Explorer window, click the + sign next to the Documents folder (under Libraries) and then click the + sign next to the My Documents folder displayed. In the list you will see a folder called Arduino, which was created when the Arduino IDE was installed earlier. Arduino Library
  5. Drag both folders in the right pane (libraries and AferoMCULab) to the Arduino folder shown in the expanded list on the left. Arduino Files
  6. You will be warned that there is an existing folder named libraries and asked if you want to merge the two. Select the Do this for all current items checkbox at the bottom of the window, then click Yes. Confirmation of Folder Replace
  7. You are now ready to continue with the lab. Skip to the next section, Create Device Profile.

Arduino IDE Setup for macOS

  1. Open a Finder window by clicking the Finder icon on the left side of the dock. Click the Downloads folder and then double-click the file you downloaded earlier. A new folder named ArduinoLab will be created in your Downloads folder. Double-click that folder to open it, and you will see two folders named libraries and AferoMCULab. Lab 3
  2. Select both folders, right-click, then select Copy 2 Items. Lab 3
  3. In the left pane of the Finder window, click the Documents folder. In this folder you will see a folder named Arduino. This folder was created when the Arduino IDE was installed earlier. Right-click the Arduino folder, and select the menu option Paste 2 Items. Lab 3
  4. You will be warned that a folder named libraries already exists in this location. Select the Apply to all checkbox and then click the Merge button. Lab 3
  5. You are now ready to continue with the lab. You can skip to the section, Create Device Profile.


Short on Time?

The section below will walk you through modifying the Profile you’ve worked with in the previous lab, to add support for this MCU lab. If you’re short on time to complete this lab, and are comfortable using the Profile Editor, we’ve provided a pre-built Profile that works with this lab:

  1. Open the Profile Editor and navigate to Documents-Arduino-AferoMCULab-profiles.
  2. If you have a Modulo-2 development board, select the AferoMCULab-2 folder (use AferoMCULab-1 only if you have a Modulo-1 board!), then click Select Folder.
  3. You can then publish this Profile as-is to your Modulo and skip the rest of this section. Otherwise, continue below.

Create Device Profile

On your computer, launch the Afero Profile Editor and open the Profile you created in the previous lab. We are going to modify the Profile to support MCU connectivity and define a couple attributes, which the MCU will be able to access.

  1. In the Profile Editor left-hand Navigation pane, click Attributes.
    1. Under the Define the MCU Attributes heading, you'll see MCU Configuration. Turn on the switch (on the right) to expand the MCU Configuration window. Under Protocol, select SPI. Leave the other checkboxes unchecked.
    2. Click the + MCU Attribute button to open a new attribute window titled Unnamed Attribute 1. We will use this attribute to display the current temperature.

      Define this temperature attribute using:

      • Attribute Name: Temperature
      • Default Value: Leave blank
      • Max Size: 9
      • Data Type: UTF8S (UTF-8 String)
      • Writeable: Deselected

        The Writeable checkbox defines whether an MCU attribute can be changed from the mobile app UI. Because this attribute will contain a temperature value read from a hardware sensor, allowing the mobile app to modify it wouldn’t make sense.

    3. Select + MCU Attribute button to create another attribute for our application. We will use this attribute to switch the MCU’s output from Celsius to Fahrenheit:
      • Attribute Name: Units
      • Default Value: false
      • Data Type: BOOLEAN
      • Writeable: Selected
    4. Click Save in the upper-right corner of the window.
  2. In the Navigation pane, click UI Controls.
    1. Click + New Control to add a control using:
      • Control Type: Value
      • Attribute: Temperature
      • Default Label: Temperature Sensor
      • View Style: Pick one!
      • Input Type: Keep default selection
      • Value Options: Leave blank
    2. Click + New Control to add another, this time using:
      • Control Type: Switch
      • Attribute: Units
      • Default Label: Scale
      • View Style: Inline
      • Primary Operation: Deselected
      • Value Options, define two:
        • Value: false; Label: Celsius; Running State: Deselected
        • Value: true; Label: Fahrenheit; Running State: Deselected
    3. Click Save in the upper-right corner of the window.
  3. In the Navigation pane, click UI Control Groups.
    1. Create a new group by clicking in the groups ribbon. Name the new group “MCU Lab”.
    2. Drag the Temperature and Scale UI Controls into the area under the groups ribbon.
    3. Click Save.
  4. In the Navigation pane, click Publish.
    1. Go ahead and publish your Profile to your Modulo dev board. Ensure that the board is plugged into a USB power source and online.
    2. When the device reboots and the mobile app for the Modulo includes your new MCU Lab menu, unplug the dev board from the Micro-USB cable so we can assemble the hardware.

Assemble Arduino Hardware

  1. Remove the Arduino Uno from its packaging if you haven’t already. Remove the Plinto adapter from its packaging and carefully insert the Plinto pins into the sockets on the edges of the Uno’s circuit board. Take care that all pins are inserted properly and none are bent.
  2. Referring to the image below, take your Modulo development board and align its pins in the socket on the Plinto. The dev board USB port should be on the same side as the Arduino Uno’s USB port. The end of the dev board with the Afero and Microchip logos should overhang the edge of the Plinto a little bit. The dev board will not work properly if it’s inserted backwards.
  3. Install the MCP9700 Temperature sensor. The sensor has a flat side and a rounded side, with three pins sticking out the bottom of the sensor.

    With the flat side of the sensor facing “inward” towards the dev board, insert the temperature sensor into the Plinto board so that the three pins on the sensor are inserted into the three adjacent pins labeled A0, A1, and A2 on the left side of the Plinto.

    You may have to separate the pins on the sensor very slightly to line up the pins with the holes.

    The temperature sensor will not insert fully into the socket and will stick up around 0.25” above the Plinto socket. This is perfectly normal. Do not force the sensor “all the way” into the pins on the Plinto.

    Hardware Assembly
  4. Connect the Arduino Uno to your computer with the USB-A to USB-B cable. Windows may display a message that new device driver software is being installed.

Upload Arduino Sketch to Uno

  1. Launch the Arduino IDE.
  2. From the File menu, select Open and double-click the AferoMCULab folder. Double-click the AferoMCULab Arduino file in that folder; the code will be displayed in the IDE.
  3. In the Tools menu, select Board, then select Arduino/Genuino Uno as the board type. Lab 3
  4. In the Tools menu, select Port, then select Arduino/Genuino Uno as the port (the COM port number will be different). Device Type Window
  5. Select the Sketch menu, and then select Upload to compile the code and upload it to the Uno board. Lab 3
  6. When you see the message “Done uploading” at the bottom of the screen, select the Tools menu and then Serial Monitor. Change the baud rate in the lower-right corner of that window to 38400. You should see messages from the application in a few moments, including a temperature reading every few seconds. Lab 3
  7. The lab code you just uploaded is a standalone application, like an application you may have that performs a task you might want to connect to the cloud.
  8. Now let’s modify the application to send temperature data to the Afero Cloud.

    Scroll down in the application code a few screens until you see the following section of code in the loop() function:

    /* HINT uncomment this section for lab part one */
    int rc = 0;
      char str[9];
      if (farenheit) {
        sprintf(str, "%f.f", toFarenheit(currentTemp));
        rc = af_lib_set_attribute_str(af_lib, AF_TEMPERATURE, strlen(str), str);
      } else {
        sprintf(str, "%f.f", currentTemp);
        rc = af_lib_set_attribute_str(af_lib, AF_TEMPERATURE, strlen(str), str);
      if (rc != AF_SUCCESS) {
        Serial.print(F("Couldn't send Temperature update, rc="));
    /* HINT end */

    Remove the “//” (two slashes) from the 10 lines of code between the /* HINT */ comments, as shown above.

  9. This code handles the selection of Celsius and Fahrenheit, and does a little error checking, but the important part of this code is the line:

    rc = af_lib_set_attribute_str(af_lib, AF_TEMPERATURE, strlen(str), str);

    This call to af_lib_set_attribute_str is all that is needed to take the sensor data that we’re already reading (currentTemp is the value of the hardware temperature sensor) and send it to the Afero Cloud.

  10. Scroll down a couple of screens in the code again until you see the following section of code in the attrEventCallback() function:

    /* HINT uncomment this section for lab part two */
    case AF_UNITS:
      // if UNITS attribute is 1/true, then display temps in F instead of C
      farenheit = (*value == 1);
      af_lib_send_set_response(AF_UNITS, true, valueLen, value);
    /* HINT end */

    Remove the “//” (two slashes) from the four lines of code between the /* HINT */ comments, as shown above.

    attrEventCallback is called when the attached Modulo receives an update from the Afero Cloud. This function is called to tell your application that some action should occur based on the eventType and the specific attribute value. In this case, we compare the value of the attribute to “1” (or “true”, since it’s a Boolean value); and if it’s “true”, we instruct the Arduino code to convert the temperature sensor value into degrees Fahrenheit from Celsius.

  11. Once you’ve made these changes to the code, select Upload from the Sketch menu again, and after the code is compiled and uploaded, look at the device on the mobile app. You should see the temperature being updated every few seconds on the mobile app, and if you change the switch from Celsius to Fahrenheit, the next sensor reading a few seconds later will switch to the updated unit.

Extra Credit

At the very bottom of the Arduino code is a debug method called printAttribute(), which can be used to print out attribute values as they’re received from the Modulo.

Create a new MCU attribute or two in the Profile Editor, assign UI controls to them, and then add code in printAttribute() to print out their values as they’re received from the Afero Cloud.

You don’t have to add any code to the Arduino sketch if you don’t want to, but a little debug code here will demonstrate the variety of data types you can communicate between the Afero Cloud and your MCU.