littleBits is a set of electronic components that magnetically connect together. litteBits is geared towards the kids STEP market and it is available in many schools and libraries.
The littleBits company has done an excellent job making their product easy to use. There is a large variety of different “bit” modules and for Internet applications there is a Cloud Bit ($59).
I found that the Cloud Bit was very easy to get up and running, but I found it was expensive at $59 and somewhat limiting, for example you are only access 1-input and 1-output. So if you want to do 2 inputs/output you would need to purchase a second Cloud bit module.
In this blog I’d like to document how I used a $39 Arduino Bit to do 3-inputs and 3-outputs. I also had the code talk directly to a free Web Dashboard (AdaFruit).
littleBits Arduino Program
A set of commands needs to be setup between the littlebits Arduino module and the PC/Pi. In my Arduino program I referenced the ports A,B,C as inputs (on the left side), and D,E,F as outputs (on the right side).
The commands from the PC/Pi would be : reference_pin:value, for example D:255 would set the D (top left pin) at 100%. It’s important to note that Arduino inputs and outputs are scaled from 0-255.
For inputs the littleBits would send the results as pin: reference_pin:value, for example B:255 would be the result at full scale for the A0 input.
My test setup had:
- A fork bit – this meant I only needed 1 power input source
- A slider bit (0-1) on bit D0 (A)
- A dimmer bit (0-255) on bit A0 (B)
- A temperature bit on bit A1 (C)
- An LED on bit d1 (D)
- A number bit on D5 (E)
- a bargraph bit on D9 (F)
Below is the littleBits Arduino program that managed the serial communications.
// littleBits_2_Dashboards - create a serial interface to read/write to a PC/Pi // // Command from the littleBits: (A,B,C are the left pins) // A:value <- for example B:24, pin A0 (2nd input) is 24 // Commands from the PC/Pi: (D,E,F are the right pins) // D:output <- for example E:128, set pin A0 to 50% (128/255) // String thecmd; String thevalue; String theinput; char outstring[3]; void setup() { //define the littleBits right side pins 1,5 and 9 pinMode(1, OUTPUT); pinMode(5, OUTPUT); pinMode(9, OUTPUT); // define the littleBits left side inputs pinMode(0, INPUT); pinMode(A0, INPUT); pinMode(A1, INPUT); Serial.begin(9600); // this needs to match the PC/Pi baud rate } void loop() { if (Serial.available() > 0) { thecmd = Serial.readStringUntil("\n"); if (thecmd.length() > 2) { // ensure the msg size is big enough thevalue = thecmd.substring(2); if (thecmd.startsWith("D")) { analogWrite(1,thevalue.toInt()); } if (thecmd.startsWith("E")) { analogWrite(5,thevalue.toInt()); } if (thecmd.startsWith("F")) { analogWrite(9,thevalue.toInt()); } } } // Try 3 different inputs: d0 = on/off , A0 = pot, A1 = temp sensor sprintf(outstring,"%d",digitalRead(0)); Serial.write("A:"); Serial.write(outstring); Serial.write("\n"); sprintf(outstring,"%d",analogRead(A0)); Serial.write("B:"); Serial.write(outstring); Serial.write("\n"); // A1 is an "i12" littleBits temperature sensor int temp = analogRead(A1); temp = map(temp,0,1023,0,99); //rescale. Sensor range is 0-99 C or F sprintf(outstring,"%d",temp); Serial.write("C:"); Serial.write(outstring); Serial.write("\n"); delay(5000); }
The Arduino IDE “Serial Monitor” can be used to view the output and set values.
Python on the PC or Raspberry Pi
The Arduino program will send input data for A,B,C every 5 seconds. This input can be seen in Python by:
# # littleBits Read Test # import serial ser = serial.Serial(port='/dev/ttyACM1', baudrate=9600) # format for Linux #ser = serial.Serial(port='COM1', baudrate=9600) # format for Windows while True: inline = ser.readline() inline = inline.decode() # make a string pin = inline[0:1] # the first character is the pin thevalue = inline[2:-1] # the value is between ":" and "\n"<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span> print(pin,thevalue)
The output will look something like:
A 1 B 1023 C 21
To write commands from Python:
Write # # littleBits Write Test # import serial ser = serial.Serial(port='/dev/ttyACM2', baudrate=9600) # format for Linux #ser = serial.Serial(port='COM1', baudrate=9600) # format for Windows while True: print("\nWrite an output value to littleBit") out = input("Enter pin:value, pin=A,B,C example: 'E:255' : ") out = out.upper() + "\n" out2 = out.encode('utf_8') ser.write(out2)
Adafruit Dashboards
There are lots of good free dashboards. For this project I used the Adafruit site. To get started you will need to log in and create a free account.
I’ve bought a number of components from Adafruit. I think that they are an excellent company that goes out of their way to create great user guides and products.
To get started with Adafruit Dashboards see: https://github.com/adafruit/Adafruit_IO_Python
The first step is to add some Adafruit tags that the code can read/write to.
In the Python code a couple of dictionaries (lb_inputs, lb_outputs) were created to link the littleBit references (A-F) with the Adafruit tags. Also two dictionaries (lb_inlast, lb_outlast) are used to minimize communications traffic so that only new values were written.
# # Import standard python modules import time, random import serial # import Adafruit IO REST client from Adafruit_IO import Client, Feed, RequestError ADAFRUIT_IO_USERNAME = "put_your_username_here" ADAFRUIT_IO_KEY = "c039f24ecb6...xxxxx" aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) # Create dictionaries of inputs, output, and last values lb_inputs = {"A":"lb-slide", "B":"lb-dimmer","C": "lb-temp"} lb_inlast = {"A":0, "B":0,"C": 0} lb_outputs = {"D":"lb-led", "E":"lb-number", "F":"lb-bar"} lb_outlast = {"D":0, "E":0,"F": 0} # Setup the serial port connection ser = serial.Serial(port='/dev/ttyACM1', baudrate=9600) while True: # Get values from littleBits and write to the dashboard inline = ser.readline() inline = inline.decode() #inline should look like: A:125\n pin = inline[0:1] # pin is the first character in string thevalue = inline[2:-1] # value is between ":" and "\n" if lb_inlast[pin] != thevalue: # Write only new values print(pin,thevalue, lb_inputs[pin]) ada_item = aio.feeds(lb_inputs[pin]) aio.send(ada_item.key,thevalue) lb_inlast[pin] = thevalue thetag = 'lb-slide' # Write new dash values to littleBits if they've changed for lbtag, dashtag in lb_outputs.items(): print (lbtag,dashtag) thevalue = aio.receive(dashtag).value if lb_outlast[lbtag] != thevalue: # Write only new values outstr = lbtag + ":" + thevalue + "\n" print(outstr) ser.write(outstr.encode('utf_8')) lb_outlast[lbtag] = thevalue time.sleep(2)
If everything is working correctly then new values should be written to in both directions. On the Adafruit Web site the Feed page should show the new values.
To make things look more presentable Adafruit Dashboards can be used.
Final Comments
In this project I used the Adafruit API, other good platforms would be IFTTT and Node-Red