Build cool stuff with littleBits, a Pi, and some Lego Bricks

Arduino Programs

Once the Arduino IDE is installed on the PC, you can start writing littleBits Arduino applications. In the Arduino desktop application, you need to set the board type by clicking Tools | Board; then, select Arduino Leonardo. To choose the serial port on the PC that talks to the Arduino, click Tools | Serial Port and select the appropriate COM port from the list. For our PC setup it was COM33.

As a first test, we used the littleBits Wires modules to connect the motors to the d1 and d9 connectors on the Arduino module, and then we wrote a program to start and stop both motors (Listing 1) [4]. Lines 4 and 5 set up the connectors as outputs, and the loop in lines 7-15 starts and stops the motors. To upload this sketch to the Arduino, click the Upload arrow in the IDE toolbar.

Listing 1


01 // Simple Motor Test
02 void setup() {
03   //define the pins 1 and 9 for the motors
04   pinMode(1, OUTPUT);
05   pinMode(9, OUTPUT);
06 }
07 void loop() {
08       // run the motors for 2 sec on stop for 2 sec
09       digitalWrite(1, 1);
10       digitalWrite(9, 1);
11       delay(2000);
12       digitalWrite(1, 0);
13       digitalWrite(9, 0);
14       delay(2000);
15 }

The next step was to get serial communications working so we could send commands from the keyboard (Listing 2). The statement Serial.begin(9600); in line 9 defines serial communication at 9600 baud, and the statement (line 14) reads the input from the USB port. The commands that control the motors (lines 16-35) are:

  • s – stop both motors
  • g – go, run both motors
  • l – turn left
  • r – turn right

Listing 2


01 // Serial Command Test
03 char thekey;
05 void setup() {
06   //define the pins 1 and 9 for the motors
07   pinMode(1, OUTPUT);
08   pinMode(9, OUTPUT);
09   Serial.begin(9600);
10   Serial.println("littleBits Arduino Rover Control");
11   Serial.println("Enter a command : s - stop , g - go \
      , l - left, r - right");
12 }
13 void loop() {
14   if (Serial.available() > 0) {
15     thekey =; // get the key from the phone
17     //  "s" stop both motors
18     if (thekey == 's') {
19       digitalWrite(1,0);
20       digitalWrite(9,0);
21     }
22     //  "g" run both motors
23     if (thekey == 'g') {
24       digitalWrite(1,1);
25       digitalWrite(9,1);
26     }
27     //  "l" only run right motor, turn left
28     if (thekey == 'l') {
29       digitalWrite(1,1);
30       digitalWrite(9,0);
31     }
32     // "r" only run left motor, turn right
33     if (thekey == 'r') {
34       digitalWrite(1,0);
35       digitalWrite(9,1);
36     }
37   }
38 }

The complete code for the Arduino module is fairly simple, but it could be expanded to support commands to the d5 connector to control lights or play music, for example.

To test serial communications, open up the Serial Monitor with the toolbar icon on the far right (Figure 5), type one of the control commands, and click Send.

Figure 5: Test commands in Arduino IDE.

Raspberry Pi Code

After we proved that we could control the littleBits motors from the Arduino monitor, we wanted to send the same commands to the Arduino using the Raspberry Pi (Figure 6).

Figure 6: Driving the rover remotely from the Raspberry Pi.

The first step was to ensure that the Rasp Pi could see the littleBits Arduino module on the USB cable. The Linux command lsusb should find the module. On the Raspberry Pi, the USB port is named /dev/ttyACM0.

The small Python program in Listing 3 sends commands to the rover. Text editors like Nano can be used to create the Python files.

Listing 3


01 import serial
03 port = serial.Serial("/dev/ttyACM0", 9600)
05 keycode = " "
06 print "Drive the Rover from Pi"
07 print "Commands: g=go, s=stop, l=left, r=right, or x=exit"
08 while keycode <> "x":
09    keycode = raw_input("Enter a command: ")
10    port.write(keycode)

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Make an Android phone app to control your littleBits projects

    Make a remote control paddleboat with the littleBits Arduino module, DC motors, and generic Bluetooth module and control the boat with a phone app that uses the MIT App Inventor package.

  • Control your littleBits projects with a homemade wireless remote

    Make a custom handheld wireless remote control with littleBits Wireless Transmitter and Receiver bits and slider, knob, button, or toggle bits.

  • Infinite Possibilities

    The open hardware movement continues to add, improve, and reinvent itself. No sooner did the Raspberry Pi Model B+ come on the scene, than the Raspberry Pi 2 debuted, adding a "1" to the name of all Rasp Pis that came before. Faster, smaller, and smarter devices appear almost daily, inspiring makers as they address ever more diverse real-world problems.

  • Build cool stuff with Lego and Pi

    By combining your Lego Mindstorms NXT, a Raspberry Pi, and some stuff from around the house, you can make some cool projects.

  • SunRover Part 2 – Solar Power Controller/Power System

    Putting power in your robot is more than just running wires – you'll need to make sure the power supply doesn't cause interference that will disrupt other components. This article explores the problem of electrical noise and describes the design for a multiplexing solar power system.