Overview
The Past
Before the advent of computers, all data was written in notebooks. One of Leonardo da Vinci's notebooks, above, is an example, as are the notebooks by the great scientists of the past.
The Present
While observation by eye is still used today, more often than not we automate readings by electronics. In any given day, billions of sensors worldwide take readings of heat, light, wind, etc. But where does that data go?
The Spreadsheet
To record and analyze readings, it is easiest for most folks to use a spreadsheet. Available on all computer platforms, a spreadsheet is one of the first programs people learn. And, the analytical capabilities (re. plotting/graphing) are excellent.
This guide will quickly allow you to learn about placing data readings directly into the spreadsheet of your choice, A Circuit Playground Express and it's multitude of sensors, will log data to a spreadsheet and an Android phone.
For the plant monitoring example - there is an all-in-one kit or if you already have an Circuit Playground Express, add alligator clips:
or
Or a standard USB OTG cable adapter
Page last edited March 08, 2024
Text editor powered by tinymce.
Logging Into a Spreadsheet
There are several ways to record data into a spreadsheet. The following examples will have Circuit Playground Express poll its sensors and then output the data over USB in such a way that it will "type" the numbers into a on-screen computers spreadsheet.
This method uses the USB feature known as human interface device protocol (HID). Plug in a keyboard or mouse into modern computers via USB, and they "just work". Computers can have multiple keyboards, it doesn't confuse them. So we'll use this property to have the Circuit Playground Express output data like a keyboard would, "typing" characters out.
For spreadsheets, to go "to the next line" the keystrokes are typically the down arrow followed by the left arrow to get back to column 1. We'll emulate those keystrokes also to align the data into nice, neat columns.
The first examples shows logging data via CircuitPython. The second example demonstrates the same concept in Microsoft MakeCode.
Page last edited March 08, 2024
Text editor powered by tinymce.
CircuitPython
CircuitPython
This program is written in CircuitPython, the easy to use language for microcontrollers. If you'd like to learn more about CircuitPython and how to easily get started, please read this guide.
Adafruit recommends the Mu editor. Available for PC, Mac, and Linux for free, it provides a clean, easy to use interface and code upload directly to Adafruit boards. You can learn about Mu here.
How it Works
The following program will take three readings:
-
time relative to the power-on of the board, not time of day - this is what we get when we request
time.monotonic() -
light intensity - this is not in lux but it does have higher numbers when theres more light. This is what we get when we request
light.value -
temperature in Celsius (unless you uncomment one line to get Fahrenheit). This is what we get when we request
thermistor.temperature
Once we've collected those three data points, the values are written out via Keyboard emulation to your computer.
When the slide switch is closed, moved toward the black speaker, it does not log data. When the switch is moved towards the microphone, data will be "typed" out by the board and populate your spreadsheet. Moving the switch back stops the logging so you can use your regular keyboard without numbers being injected into your work.
Start with the switch closed (to the left)
The Code
Copy the following code into Mu. Then save the code to the Circuit Playground Express as code.py. The program runs immediately, so be sure to switch the closed as explained above!
# SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# Circuit Playground Express Data Time/Light Intensity/Temp
# Log data to a spreadsheet on-screen
# Open Spreadsheet beforehand and position to start (A,1)
# Use slide switch to start and stop sensor readings
# Time values are seconds since board powered on (relative time)
import time
from digitalio import DigitalInOut, Direction, Pull
import analogio
import board
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
import adafruit_thermistor
# Switch to quickly enable/disable
switch = DigitalInOut(board.SLIDE_SWITCH)
switch.pull = Pull.UP
# light level
light = analogio.AnalogIn(board.LIGHT)
# temperature
thermistor = adafruit_thermistor.Thermistor(board.TEMPERATURE, 10000,
10000, 25, 3950)
# Set the keyboard object!
# Sleep for a bit to avoid a race condition on some systems
time.sleep(1)
kbd = Keyboard(usb_hid.devices)
layout = KeyboardLayoutUS(kbd) # US is only current option...
led = DigitalInOut(board.D13) # Set up red LED "D13"
led.direction = Direction.OUTPUT
print("Time\tLight\tTemperature") # Print column headers
def slow_write(string): # Typing should not be too fast for
for c in string: # the computer to be able to accept
layout.write(c)
time.sleep(0.2) # use 1/5 second pause between characters
while True:
if switch.value: # If the slide switch is on, don't log
continue
# Turn on the LED to show we're logging
led.value = True
temp = thermistor.temperature # In Celsius
# if you want Fahrenheit, uncomment the line below
# temp = temp * 9 / 5 + 32
# Format data into value 'output'
output = "%0.1f\t%d\t%0.1f" % (time.monotonic(), light.value, temp)
print(output) # Print to serial monitor
slow_write(output) # Print to spreadsheet
kbd.press(Keycode.DOWN_ARROW) # Code to go to next row
time.sleep(0.01)
kbd.release_all()
for _ in range(3):
kbd.press(Keycode.LEFT_ARROW)
time.sleep(0.015)
kbd.release_all()
time.sleep(0.025) # Wait a bit more for Google Sheets
led.value = False
# Change 0.1 to whatever time you need between readings
time.sleep(0.1)
The Spreadsheet
You should have your computer open to a spreadsheet where you want the data (often cell A1). You can use Excel, Google sheets, OpenOffice Calc...anything you like!
Use
When the slide switch is to the right, the data outputs the characters for the time, light intensity and temperature to the screen. Tabs are used to go from cell to cell and emulated cursor keys to get to the first cell of the next row.
When you are done logging, move the slide switch left towards the square black speaker.
If you want more delay between readings, see the bottom of the code to place a call to time.sleep with the parameter afterwards the number of seconds between each reading, such as time.sleep(2.0) to wait 2 seconds between readings.
If you find that the data is getting into the wrong column, adjust the delay between arrow keystrokes a tiny bit - we are "faking" typing, and the Circuit Playground Express can type faster than we can.
Graphing
Each spreadsheet is a bit different on how it graphs so you should read the guides for your spreadsheet on how to do this. Generally you highlight the data (three columns by the number of rows you want graphed and then tell the spreadsheet to plot the data.
The first value will usually be chosen as the x-axis which is good - the time value will give you a basis for consistent reading times for the plot. The light and temperature readings should be plotted as two y values with separate scales.
The following is a plot on Google Sheets:
Plotting both on the same graph is a bit limiting as the scales are very different for light and temperature. I used a logarithmic scale to adjust, but you might wish to plot each separately to see variations.
Page last edited March 08, 2024
Text editor powered by tinymce.
MakeCode
The MakeCode program acts 100% like the CircuitPython code. You will need to take a couple of steps to set up MakeCode to use Keyboard extension blocks to have the days go into individual spreadsheet cells.
Setup
As of this guide (August 14, 2018) you will need to use the beta web version of MakeCode.
https://makecode.adafruit.com/beta
As this uses the Keyboard extension which has some very new edits. They should be in the main MakeCode builds later in the month.
Select the Keyboard extension and a new block, group colored black, named KEYBOARD will be available. You can use the blocks in this group to move the arrow keys in the spreadsheet.
How it Works
The following program will take three readings:
- time relative to the power-on of the board, not time of day - this is the millis (ms) block
- light intensity - this is not in lux but it does have higher numbers when theres more light. This is the light level block
- temperature in Celsius or Fahrenheit, this is the temperature block
The values are written out via Keyboard emulation to your computer.
Here's the full MakeCode block set:
The Spreadsheet
You should have your computer open to a spreadsheet where you want the data (often cell A1). You can use Excel, Google sheets, OpenOffice Calc...anything you like!
Note that this code will not work with a word processor as-is as it assumes using arrow keys and tabs to change columns. The character 9 in the data writes is the standard code for the tab key so the numbers are written and the code will output a tab afterwards going to the next cell in the spreadsheet.
When the slide switch is closed, moved toward the black speaker, it does not log data. When the switch is moved towards the microphone, data will be "typed" out by the board and populate your spreadsheet. Moving the switch back stops the logging so you can use your regular keyboard without numbers being injected into your work.
Changes
Feel free to make changes to read other sensors. Also you can use the Pause block to slow down readings in case you believe the readings are being taken too quickly.
Additional use of Keyboard Commands
See the Adafruit tutorial Make it a Keyboard for additional keyboard examples.
Page last edited March 08, 2024
Text editor powered by tinymce.
Plant Monitoring
Adafruit's basic tutorial for monitoring soil moisture is in this guide.
This page expands that project to log plant data to a spreadsheet. It includes the soil moisture level as measured in the soil moisture tutorial via a capacitive touch pad connected to a piece of metal in the soil of the plant. Measuring the temperature and light levels gives a complete picture of the plant's progress.
The Code
The CircuitPython code is the same as the previous example with these changes:
- The soil moisture is read on a capacitive touch pad connected to a nail in the soil
- The moisture data is a fourth column in the spreadsheet.
Here is the code:
Plotting
Here I used Microsoft Excel to hold the data this time. The time units were from 613 to 698 seconds after the board was powered. That forms the x-axis values. The plots are the light (varies the most), temperature and soil moisture (which usually don't vary much over a few minutes).
Use
This code provides a complete plant monitoring solution. You can monitor the light and temperature and how moist the soil is over time.
The code could be expanded to possibly self-water. You would need a soil moisture value at which you consider the soil to be dry. We have found that 1500 is a good baseline that you can use based on the type of plant and it's requirements.
You would check if the moisture was < 1500, then do something that releases a determined amount of water (which again varies by the plant type, pot, etc.) to water the plant appropriately.
Page last edited March 08, 2024
Text editor powered by tinymce.
Logging Via Android Phone
In the tutorial Android GBoard Morse Code Control with Circuit Playground Express, Adafruit demonstrates using the HID Keyboard functions to enter data into an Android phone and then use a feature of the Google GBoard keyboard to turn Morse Code into text.
But you don't have to use Morse Code, you can plug the CPX right into your Android phone or tablet and have it type just the same way it does on your computer with a spreadsheet above. This time you can go mobile. All you need is a special USB cable called OTG (on the go).
Page last edited March 08, 2024
Text editor powered by tinymce.
Connecting Your Phone to the Board
Android phones typically come with a micro-B USB connection. This USB connection follows the USB on-the-go (OTG) specification so peripherals can be plugged in to the port and be used by the phone.
This project is going to use the capabilities of Circuit Playground Express to act as a human interface device (HID), namely a keyboard. When the user wants the Express to send a key, they'll use a button, the board will translate this to some keyboard character, and the phone will believe a keyboard was used to enter the character.
This type of behavior is useful for a great many projects. Control of devices using alternative interfaces is the most popular. This can be in manufacturing, at home, or in assistive technology (AT) situations where traditional keyboards cannot be conveniently used.
For this project you will need a micro-B male to micro-B male connection that conforms to the OTG specification. Both the phone and the Circuit Playground Express have a micro-B female connector.
The cable at left is the most direct connection. Some electronics stores carry these - Adafruit sells them as product #3610.
There are also a number of other OTG to regular USB connector devices including Adafruit Tiny OTG Adapter and the short USB OTG Host Cable - MicroB OTG male to A female.
Page last edited March 08, 2024
Text editor powered by tinymce.
Logging to the Phone
With your USB connection from the Circuit Playground to your phone set, the Circuit Playground Express data can be logged to the phone.
Spreadsheet or other Data Capture Program
You should also use the Google Play Store to install a spreadsheet (or other program of your choice) to capture data from the Circuit Playground Express acting as a HID keyboard.
As Android is Google-centric, the best option is to go to the Google App store and load Google Sheets if it is not already available. I note that Microsoft Excel is also available in the Google App Store, but we didn't try it!
Code
The code can be either of the programs earlier in this tutorial. The demonstration will use the second example which includes plant soil moisture data in the fourth column of the spreadsheet. You can change the program to add or subtract data as desired.
Page last edited March 08, 2024
Text editor powered by tinymce.