Connect littleBits components directly to your PC or Pi
Bits and Pieces
Communicate with your littleBits sensors and devices through Python programs.
The littleBits platform [1] comprises easy-to-use electronic building blocks designed for use in schools and libraries to teach kids about electric circuits; littleBits snap together with magnets, so no soldering is needed.
I purchased a couple of littleBits kits for my kids, and they built some amazing projects; unfortunately, they grew out of it faster than I expected. However, adding some Python programming to their littleBits projects opened up a lot of new possibilities.
In this article, I look at three ways you can use Python to communicate with littleBits. In the first method, I look at directly connecting littleBits to Raspberry Pis. In the second method, I use serial/USB connections. In the third method, I add an Internet connection.
Connecting to a Rasp Pi
To connect to external equipment, littleBits offers the Perf Bit, the Proto Bit, and bitSnaps (Figure 1). The Perf Bit allows for custom breadboarding and wiring within a littleBits form factor. The Proto Bit has screw-in jumper connections that let you wire external inputs and outputs. The bitSnaps are low-cost input and output terminators to which you can solder. These three components can be purchased separately or all together in the littleBits Hardware Development Kit. For most projects, I found that the Proto Bit [2] was the simplest option.
To send a digital output from the Rasp Pi, wire the Gnd, 5V, and a Rasp Pi data pin to the input side (labeled "in") of the Proto Bit (Figure 2). A simple Python example to turn on a fan connected to pin 17 is shown in Listing 1.
Listing 1
littleBits_out.py
Two Proto Bits are required for littleBits digital input circuits. The first Proto Bit supplies the power and ground, and the second Proto Bit connects the output to the Rasp Pi pin (Figure 3). It is important to connect the Pi's 5V pin to both the VCC and the data pins on the input side of the first Proto Bit. Power is required on the input data pin, or you will not see any changes on the data output pin.
Listing 2 is a Python example to read a button input. Push-button inputs often have multiple 0-1-0 transitions because of a small amount of "debouncing" in the connection points. A callback function with a debounce timeout (here, 500ms; line 10) can eliminate these extra readings.
Listing 2
littleBits_in.py
The Raspberry Pi does not natively support analog inputs, so some added hardware is required. One great Rasp Pi add-on, the Pimoroni Explorer HAT Pro [3], supports four analog inputs and two bidirectional motors. Like digital inputs and outputs, the key is to wire the Explorer HAT analog input or output data pin into the center data connector of the Proto Bit.
Using the basic digital input and output examples, you can create some interesting projects. Figure 4 is an example of an Internet radio player that uses the littleBits hardware for station selection and volume control.
Python Serial Connection
The Arduino Bit [4] supports three littleBits input connectors and three littleBits output connectors. Other connections are possible with the pins on top, but you would need to use some wiring and Proto Bits. The Arduino Bit also has a USB port that can be used for serial communications (Figure 5).
To manage the serial communication, a small C program needs to be loaded on the Arduino Bit. An example serial communications program for managing three digital outputs (d0, d5, d9) is shown in Listing 3 that reads serial commands and sets outputs. The Arduino Bit is programmed and uploaded with the standard Arduino IDE [5] over the USB connection. Once the C program is loaded to the Arduino Bit, you can exit the Arduino IDE and use the USB connection and Python programs. Listing 4 shows a Python serial test program that sends commands to toggle the three littleBits outputs.
Listing 3
littleBits_Serial C Code
Listing 4
littleBits_Serial Python Code
After you have the basic serial communications mastered, you can move on to creating some more advanced projects. Figure 6 shows a rover that was created with an Arduino bit, two DC Motor bits, and a Rasp Pi. A Python program on the Pi uses a Wii Bluetooth library so the Wii remote can control the rover.
Python and cloudBit
The cloudBit [6] connects a littleBits input and output to the Internet (Figure 7). Setting up the cloudBit is pretty straightforward, but to access the cloudBit REST API, you need to log in to the littleBits website [7] to get your specific device ID and AccessToken (Figure 8).
To enable the cloudBit API, you need to set your AccessToken into the header of your HTTP request. The device ID is passed as a parameter in the URL. The cloudBit has a number of interesting calls, but for now, I will only look at reading inputs and setting outputs.
The input value is returned as a streaming value, which can be a problem if your application is only expecting a single value. To get only a single data value, a Python break
statement can be used after reading the first line of data. An example to read only a single value is shown in Listing 5.
Listing 5
lb_input.py
For outputs, the cloudBit API expects the parameters to be formatted as JSON. The output value can be between 0 and 100, with a pulse duration defined in milliseconds. A constant output is set by passing a duration time of -1ms, as shown in Listing 6.
Listing 6
lb_output.py
Once you have the basic setup working, you can move on to more advanced applications that use the TkInter [8] GUI for Python, databases, or web interfaces.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.
-
DebConf24 to be Held in South Korea
Busan will be the location of the latest DebConf running July 28 through August 4
-
Fedora Unleashes Atomic Desktops
Fedora has combined its solid distribution with rpm-ostree system to make it possible to deliver a new family of Fedora spins, called Fedora Atomic Desktops.
-
Bootloader Vulnerability Affects Nearly All Linux Distributions
The developers of shim have released a version to fix numerous security flaws, including one that could enable remote control execution of malicious code under certain circumstances.