IoT Motion/Temperature Logger with the Analog Devices ADXL343 + ADT7410
2020-01-02 | By Adafruit Industries
License: See Original Project
Courtesy of Adafruit
Guide by Brent Rubell
Upgrade any Feather board with motion and precision temperature sensing, with the Adafruit Sensor Featherwing. It sports two fantastic sensors from Analog Devices: an ADXL343 triple-axis accelerometer and an ADT7410 precision temperature sensor.
This FeatherWing can easily be brought online using a Feather ESP8266 Huzzah and Adafruit IO - you'll be up and running in under 15 minutes!
If you're looking to for a way to monitor and send precise motion and temperature data to the cloud, follow along!
Thanks to Digi-Key and Analog Devices for sponsoring the development of this breakout board - we've made the PCB "Digi-Key red" in their honor!
You'll be using the Analog Devices ADT7410 built into the Temperature + Motion Wing to measure the ambient temperature over I2C.
You'll also be using the Analog Devices ADXL343 triple-axis accelerometer to monitor motion over I2C. The Analog Devices ADXL343 has three axes of measurements: X, Y, and Z. You can set the sensitivity level to either +-2g, +-4g, +-8g or +-16g. The lower range gives more resolution for slow movements, the higher range is good for high speed tracking.
Adafruit IO
Adafruit IO is the easiest way to stream, log, and interact with your data. It's built from the ground up to be easy to use - we do the hard stuff so you can focus on the fun stuff.
Data such as temperature and motion can be hard to visualize and quantify - Adafruit IO makes it simple. Send IO your data and it can store and display it using charts, graphs, gauges, and more!
Arduino Code and Libraries
Bring the Adafruit Sensor FeatherWing to life using Arduino libraries for both sensors on the FeatherWing - the ADXL343 and the ADT7410.
To rapidly connect the Feather Huzzah to the internet, you'll be using use the Adafruit IO Arduino library.
Prerequisite Guides
If you're new to Adafruit IO, take a moment to walk through the following guides to get you started and up-to-speed:
Parts
You're going to be working with the Adafruit ADXL343 + ADT7410 Sensor FeatherWing. There's no wiring required - connect the FeatherWing to your HUZZAH and you can start working with the FeatherWing immediately!
- Adafruit ADXL343 + ADT7410 Sensor FeatherWing
- Assembled Feather HUZZAH w/ ESP8266 WiFi With Stacking Headers
Materials
You'll need some extra supplies to finish this project. If you do not have them already, pick some up from Adafruit:
If you would like to power your IoT project wirelessly, you may want to pick up a LiPo battery, This one comes with a pre-attached 2-pin JST-PH connect so you can easily connect it to the JST on the Feather HUZZAH.
Adafruit IO Setup
Feed Setup
If you do not already have an Adafruit IO account set up, head over to io.adafruit.com to link your Adafruit.com account to Adafruit IO.
The first step is to create a new Adafruit IO feed to hold the AD7410's temperature. Navigate to the feeds page on Adafruit IO. Then click Actions -> Create New Feed, and name this feed temperature.
- If you do not already know how to create a feed, head over to Adafruit IO Basics: Feeds.
You'll also need to create three more feeds to hold the data for the ADXL343 accelerometer - accelX, accelY, accelZ.
Once you have four feeds created, move to setting up a beautiful dashboard.
Dashboard Setup
The next step is to create a dashboard to display the values read by the Motion + Temperature FeatherWing.
- If you do not know how to create or use Dashboards in Adafruit IO, head over to the Adafruit IO Basics: Dashboards guide.
Add a Gauge Block
The gauge block allows you to quickly view the current value of a numeric feed. You can set a minimum and maximum value for the gauge, and it will automatically scale the value to a percentage and display it graphically. The gauge will update automatically whenever a new value is pushed to the feed.
Select the Gauge Block
Select the temperature feed you created earlier.
In the Block Settings step, set the Block Title to Temperature, set the Gauge Min/Max Values to the upper and lower temperature thresholds you want to measure.
You can label the gauge by setting the Gauge Label - this example assumes temperature is to be measured in Degrees C.
Uncomfortably hot/cold? You can optionally set the gauge change color to warn you if the temperature goes above (or below) a certain value.
Add a Number Slider Block
The number slider allows you to quickly change the numeric value of a feed. You can set the minimum and maximum values for the slider, as well as change the amount the slider will increment when you drag the handle.
Create a new number slider block and select the accelX feed you created earlier.
Name the block Accelerometer X. From here, you can configure the minimum and the maximum values displayed by the slider.
Next, add two more sliders to the dashboard. These sliders will be linked to the AccelY and AccelZ feeds you created earlier.
Once you're finished adding the sliders, your dashboard should look like the following screenshot:Your browser does not support the video tag.
Obtain your Adafruit IO Key
You are also going to need your Adafruit IO username and secret API key.
Navigate to your profile and click the View AIO Key button to retrieve them. Write them down in a safe place, you'll need them for the next step.
Next, we'll move on to programming the Feather and using it with Adafruit IO.
Arduino Code
Installation
To read data from the Motion and Temperature FeatherWing, you'll need to install libraries for both of the ADXL343 and the ADT7410 sensors.
To install the library for the ADXL343, visit this page and come back here when you have the library installed and tested.
Next, to install the library for the ADT7410, visit this page and come back here when you have the library installed and tested.
This guide assumes you've completed the setup required to get your Adafruit Feather HUZZAH ESP8266 up and running with Arduino IDE and Adafruit IO.
- If you haven't yet set up your Adafruit Feather HUZZAH ESP8266 for use with Adafruit IO and the Arduino IDE, follow along with this guide. The setup only needs to be performed once.
// Adafruit IO - Analog Devices ADT7410 + ADXL343 Example
//
// Adafruit invests time and resources providing this open source code.
// Please support Adafruit and open source hardware by purchasing
// products from Adafruit!
//
// Written by Brent Rubell for Adafruit Industries
// Copyright (c) 2019 Adafruit Industries
// Licensed under the MIT license.
//
// All text above must be included in any redistribution.
/************************ Adafruit IO Config *******************************/
// visit io.adafruit.com if you need to create an account,
// or if you need your Adafruit IO key.
#define IO_USERNAME "YOUR_IO_USERNAME"
#define IO_KEY "YOUR_IO_KEY"
/******************************* WiFi Config ********************************/
#define WIFI_SSID "WIFI_NAME"
#define WIFI_PASS "WIFI_PASS"
// comment out the following two lines if you are using fona or ethernet
#include "AdafruitIO_WiFi.h"
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
/************************** Configuration ***********************************/
// time between sending data to adafruit io, in seconds.
#define IO_DELAY 5
/************************ Example Starts Here *******************************/
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_ADT7410.h"
#include <Adafruit_ADXL343.h>
float tempC, accelX, accelY, accelZ;
// Create the ADT7410 temperature sensor object
Adafruit_ADT7410 tempsensor = Adafruit_ADT7410();
// Create the ADXL343 accelerometer sensor object
Adafruit_ADXL343 accel = Adafruit_ADXL343(12345);
// set up the 'temperature' feed
AdafruitIO_Feed *huzzah_temperature = io.feed("temperature");
// set up the 'accelX' feed
AdafruitIO_Feed *huzzah_accel_x = io.feed("accelX");
// set up the 'accelY' feed
AdafruitIO_Feed *huzzah_accel_y = io.feed("accelY");
// set up the 'accelZ' feed
AdafruitIO_Feed *huzzah_accel_z= io.feed("accelZ");
void setup()
{
// start the serial connection
Serial.begin(115200);
// wait for serial monitor to open
while (!Serial)
;
Serial.println("Adafruit IO - ADT7410 + ADX343");
/* Initialise the ADXL343 */
if(!accel.begin())
{
/* There was a problem detecting the ADXL343 ... check your connections */
Serial.println("Ooops, no ADXL343 detected ... Check your wiring!");
while(1);
}
/* Set the range to whatever is appropriate for your project */
accel.setRange(ADXL343_RANGE_16_G);
/* Initialise the ADT7410 */
if (!tempsensor.begin())
{
Serial.println("Couldn't find ADT7410!");
while (1)
;
}
// sensor takes 250 ms to get first readings
delay(250);
// connect to io.adafruit.com
Serial.print("Connecting to Adafruit IO");
io.connect();
// wait for a connection
while (io.status() < AIO_CONNECTED)
{
Serial.print(".");
delay(500);
}
// we are connected
Serial.println();
Serial.println(io.statusText());
}
void loop()
{
// io.run(); is required for all sketches.
// it should always be present at the top of your loop
// function. it keeps the client connected to
// io.adafruit.com, and processes any incoming data.
io.run();
/* Get a new accel. sensor event */
sensors_event_t event;
accel.getEvent(&event);
accelX = event.acceleration.x;
accelY = event.acceleration.y;
accelZ = event.acceleration.z;
/* Display the results (acceleration is measured in m/s^2) */
Serial.print("X: "); Serial.print(accelX); Serial.print(" ");
Serial.print("Y: "); Serial.print(accelY); Serial.print(" ");
Serial.print("Z: "); Serial.print(accelZ); Serial.print(" ");Serial.println("m/s^2 ");
// Read and print out the temperature
tempC = tempsensor.readTempC();
Serial.print("Temperature: "); Serial.print(tempC); Serial.println("C");
Serial.println("Sending to Adafruit IO...");
huzzah_temperature->save(tempC, 0, 0, 0, 2);
huzzah_accel_x->save(accelX);
huzzah_accel_y->save(accelY);
huzzah_accel_z->save(accelZ);
Serial.println("Data sent!");
Serial.print("Waiting ");Serial.print(IO_DELAY);Serial.println(" seconds...");
// wait IO_DELAY seconds between sends
for (int i = 0; i < IO_DELAY; i++)
{
delay(1000);
}
}
Code Setup
Before you upload the sketch to the Huzzah, you'll need to configure the code for your network and Adafruit IO.
Change IO_USERNAME to your Adafruit IO username.
Then, change IO_KEY to your Adafruit IO Key.
Next, we'll configure the code for your router. Change WIFI_SSID to your router's SSID and WIFI_PASS to your router's password.
Save the code (CTRL/CMD + S). Then, verify that the sketch compiles (Tools -> Verify).
Upload the sketch to your board (CTRL/CMD + U).
Open the Arduino Serial Monitor. The serial monitor should output that the sensors have been initialized and that the Feather Huzzah has connected to Adafruit IO:
Adafruit IO - ADT7410 + ADXL343
ADXL343 Initialized
ADT7410 Initialized!
Connecting to Adafruit IO: AdafruitIO::connect()
.
Adafruit IO connected.
The Huzzah reads temperature values from the ADT7410 and the accelerometer values from the ADXL343:
X: -0.55 Y: 0.12 Z: 9.85 m/s^2
Temperature: 28.69C
And sends these values to Adafruit IO...
Sending to Adafruit IO...
Data sent!
Waiting 15 seconds..
The delay between sending this data is configurable (modify the DELAY_SECONDS definition at the top of the code to change it). If it's sending data too frequently (above 30 data points a minute for Adafruit IO Free users), you can increase the delay between sends.
Code Usage
Let's check that the data has been received by Adafruit IO. You can do this by visiting the Adafruit IO Monitor page. Every time the Huzzah sends data to Adafruit IO, this page shows the temperature, accelX, accelY or accelZ values.
Navigate to the dashboard you created earlier. You'll notice the Gauge block change values whenever the ADT7410 reports a new temperature to the Huzzah. As you tilt the Huzzah, you'll notice the sliders for X, Y, and Z changing their positions to reflect the ADXL343's values in m/s^2.
Taking it Further
Data Visualization
While the sliders can provide immediate information about the ADXL343 or the ADT7410 on the FeatherWing at a glance - you may want to chart your data over a longer period of time or see a list of data with previous values on the dashboard.
Connect a Line Graph Block to the the four feeds to view data from the sensors over any period of time - from real-time to 30 days.
Then, add a Stream Block and connect to any feed which you'd like to monitor. This block is a miniature version of the monitor page, but only displays feeds which you specify.
Adding a Battery
Want to run your Feather without an attached USB Cable?
For 100% wireless motion and temperature logging, pick up a Lithium Ion Polymer battery (we recommend this one - it's ideal for the Feather family) and connect it to the JST-PH port on the Feather ESP8266.
Downloads
Files:
Schematic
Fab Print
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum