Skip to content

Project 1: Basics of Measurement

Daniel J. Breton edited this page Jan 21, 2020 · 3 revisions

Setup the CPX

Setting up the CPX to make a measurement is pretty straightforward.

  1. Plug in CPX
  2. Start up Mu and set it for "Adafruit" mode
  3. Bring up the serial terminal
  4. Bring in important hardware and software libraries by entering
    1. from adafruit_circuitplayground import cp
    2. import time.

After this, you should be good to go.

Voltage and Analog to Digital Conversion

Much of the material in this section can be found here

What is voltage?

How to do computers count and store numbers?

Binary numbers, of course: ones and zeroes. This means numbers of base 2, compared to the kinds of numbers we usually use which are base 10 or decimal numbers.

A single bit is the smallest amount of information any computer can store: it is a single number, and it can either be 1 or 0. If we want to count higher, we have to use more bits, just as we do when we want to count higher than 9 in decimal numbers: we have to use another digit.

So let's talk about four-bit numbers, which are, by the way, called a nibble. The way this works is that each bit represents a different power of two, and the further to the left that bit is, the more significant it is.

binary math decimal
0000 0x23 + 0x22 + 0x21 + 0x20 0
0001 0x23 + 0x22 + 0x21 + 1x20 1
0010 0x23 + 0x22 + 1x21 + 0x20 2
0011 0x23 + 0x22 + 1x21 + 1x20 3
1010 1x23 + 0x22 + 1x21 + 0x20 10
1111 1x23 + 1x22 + 1x21 + 1x20 15

So our 4-bit nibble can store numbers as small as zero, and as large as 15. We can try it out on the CPX:

>>> 0b0001
1
>>> 0b0101
5
>>> 0b1000 + 0b0010
10

What is so important about a nibble? The nibbles is "half" the size of the most famous amount of computer storage, the byte! Bytes are 8-bit numbers are really important because it generally takes one byte to store one letter (or number, or punctuation mark) in a file. Bytes are so important that all of our file sizes are described in multiples of bytes: kilobytes (kB), megabytes (MB) and sometimes even gigabytes (GB). A byte can store numbers between zero and 255, so the nibble isn't really "half" a byte, but instead has half the number of bits.

Before we leave this section, we will calculate a few more powers of two so that we can talk about even bigger numbers, like 12-bit and 16-bit numbers:

  • 212 = 4096
  • 216 = 65536

What does an analog-to-digital converter (ADC) do?

# bring in some libraries to help us talk with the hardware
import board
import analogio

# create an instance of the AnalogIn class called 'adc' which
# will measure voltage at pin A0 of the CPX
adc = analogio.AnalogIn(board.A0)

adc.value # reads voltage at pin A0
>>> def adc_to_voltage(val):
...    return val / 65535 * adc.reference_voltage  # return value in Volts
# adc_to_voltage(adc.value)

Light Measurements

Taking a single measurement

This is as simple as cp.light, which reads the value of the light sensor (look for the "eyeball" symbol on the CPX board). It should return a value somewhere between zero (if you're doing this inside a dark closet) and 300 (if you're shining a flashlight directly on the sensor. While you can hit the "up arrow" key and run cp.light again and again manually, this becomes tiresome pretty quickly, and looking at raw numbers is boring.

Can we make the CPX make repetitive measurements for us? Can we get Mu to plot these results in real time? The answer to both is 'yes'.

Taking lots of measurements

To do this, we will use a while loop which runs forever, like so:

while True:
  print("(%d)" % cp.light)  # print properly formatted data
  time.sleep(0.2)           # pause between measurements to avoid data flood

The result should be a series of numbers scrolling by vertically on the serial monitor. Once that is going, you can hit the "Plotter" button on Mu and obtain a real-time plot of the measured light intensity.

Accuracy, Precision and Uncertainty

To get at the main point of this exercise, we ask the question: how much do we believe in the answer we get from a single reading?

A short dataset of mine from a fairly dark room looks like this:

image

Mainly 3's but occasionally some 2's and 4's are thrown in there. Why? Why do our data fluctuate, when it appears (to our eyes, at least) that the lighting is quite steady? There are at least two answers:

  • In the end, the CPX is simply measuring a voltage and the voltage being measured always has some noise associated with it.
  • Not all lights are actually ON all the time. Incandescent bulbs have a constant glow, but florescent bulbs turn ON and OFF over 60 times per second, and some dimmable LED light sources are the same way. The CPX measurement happens very quickly, and if it happens to fall during a time where the light source is off, even momentarily, it may detect a much lower light signal. If the next measurement occurs when the light source is on, then we can see large and rapid changes in the observed light intensity.

What could we do to make our results more believable?