Connect littleBits components directly to your PC or Pi

Bits and Pieces

© Lead Image © Eduard Peter, 123RF.com

© Lead Image © Eduard Peter, 123RF.com

Article from Issue 217/2018
Author(s):

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.

Figure 1: Perf Bit, Proto Bit, and bitSnaps.

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.

Figure 2: A Rasp Pi controlling a littleBits fan.

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.

Figure 3: Push-button digital input.

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.

Figure 4: Python and littleBits Internet radio player.

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).

Figure 5: Arduino Bit with one input and three output connections.

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.

Figure 6: Wii-controlled littleBits 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).

Figure 7: cloudBit inputs and outputs.
Figure 8: cloudBit Device ID and AccessToken.

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

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Pi Control of USB Devices

    Command-line tools and Node-RED on a Raspberry Pi let you control projects that use the USB ports.

  • Serial Communications

    We explore serial communications, from the electrical specs to protocols and libraries, with an example of serial communication with an Arduino.

  • Repurposed Router Projects

    If you have an old router lying around, you can put it to good use with a few easy projects and learn something along the way.

  • Pluggable Pi Systems

    Ecosystems with pluggable Raspberry Pi modules, sensors, and displays are a great choice if you don't want to – or can't – solder but still want to extend your hardware.

  • Perl: Arduino Project

    With a few lines of self-written firmware and a simple Perl script, Perlmeister Mike Schilli trains one of his Linux computers with a plugged in Arduino board to switch electrical equipment on and off using a relay. Enchanting.

comments powered by Disqus
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.

Learn More

News