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 January 22, 2025
Text editor powered by tinymce.