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
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs