Some sensors can provide useful real-time data, but sometimes this data can be volatile and prone to wild swings in environmental conditions. In this how-to, we will look at the average element and how it can be used in IoT projects.
In this article, we will be looking at a simple sensor example, whereby the instantaneous values are averaged to provide a reading that is less volatile to sudden swings in sensor readings. The core of the example will be based around the Huzzah32 and the TEMT6000 ambient light sensor to provide an average light reading. The TEMT6000 is a simple sensor to interface with, but there are many potential applications for using averages including:
The hardware for this example is incredibly simple and only uses an Adafruit Huzzah32, a TEMT6000 module, three wires, and a breadboard used to hold the circuit together. The TEMT6000 sensor is an analog device that includes the light sensor as well as the resistor needed to produce the varying voltage. This resistance, 10KΩ by default, is also pre-programmed into the Digi-Key IoT studio.
The device software is very simple and only consists of four elements, including the interval element, the TEMT6000 element, the BLE Characteristic element, and the GPIO element. By default, the TEMT6000 element is not included in the Element Toolbox, so this needs to be added. To do this, click the Add Element button at the bottom of the Element Toolbox, and in the popup window that appears, search for TEMT. The only result should be the TEMT6000 element, and to insert it into the project, click the toggle slide button under enable.
The interval element is connected to the TEMT6000 element, and this connection needs to trigger the Read Ambient Light (Lux) event. The TEMT6000 element is then connected to the BLE element as well as the GPIO element. Both elements need to have their triggers changed to Ambient Light Read (Lux), so that they will trigger the connected elements when the TEMT6000 is read from. The BLE element needs to have its value set by the TEMT6000, while the GPIO pin is made to toggle. The reason for using the GPIO element like this is that it gives us a visual cue that the Huzzah32 is functioning correctly, should our example fail to produce a result and require debugging. The GPIO element needs to be configured to use GPIO 13 and be made to be an output push-pull, as the Huzzah32 includes an onboard LED connected to GPIO 13.
The app side of this project is where the average element is used. The app consists of 5 elements including the interval element, the average element, and two labels. Once every second, the interval element probes the BLE element for the latest data, and when this latest data is retrieved, it is printed to a label element.
This data is also appended to the string “Current value,” so we know which label is showing off the latest value. At the same time, the BLE element is also connected to the average element, and this connection triggers a Push event on the average element. But how does the average element work?
The average element takes n number of samples and then averages them together to produce an average result. This number, n, can either be defined at the start or changed on the go, and the element will only average the numbers in its buffer once all n values have been received.
For example, if the average element is set to average 5 results than the average value will be 0 until all five values have been passed (i.e., it is not a moving average). The average element can also be used in a different number of ways. For example, a set of values can be passed to this element directly and averaged without needing to push values. This can be useful with situations involving buffers, especially for those who want to perform moving averages as well as rolling averages. Accessing the average value can be done using the “average” variable (in a similar way to value for other elements).
We’ll continue to look at other functions, other sensors, other boards, and other possibilities in the Digi-Key IoT Studio in upcoming posts!