Arduino on Raspberry Pi part two

Use the power of Arduino to do otherwise impossible projects with just a Raspberry Pi alone

In the previous project we showed you how you can use an Arduino microcontroller to help the Raspberry Pi become proficient in new skills that can drastically increase the reach of your projects. With the aid of the extra 12 pins capable of PWM and analogue input, you could easily add multiple servos, analogue sensors and even add input devices like joysticks.

In this project we’re going to demonstrate this by creating a three-colour lamp that employs three potentiometers (twisty knobs) to control each of the three colours in an RGB LED light. With it you can make most of the colours of the rainbow. As you’d expect, this would be much more difficult using just a Raspberry Pi alone.

This is a great prototype for an attractive RGB lamp – a great night light or mood piece
This is a great prototype for an attractive RGB lamp – a great night light or mood piece


Arduino Uno
Set of prototyping cables
RGB LED (cathode)
3x potentiometers
3x 330 Ohm resisters
Tutorial files


Step 01 Program with Arduino

You’ll need to have followed the steps from with the previous project to correctly configure your Raspberry Pi and Arduino Uno. You’ll also need to ensure you’ve got all the components from the list to the left. The resistors should be 330-ohm ideally, but can be of a higher resistance if that’s all you have available.

Arduinos can be bought as part of ‘starter packs’ that include exactly these kinds of components, but a quick visit to
CPC should fill any holes.

Step 02

Populate the breadboard

The circuit for this project might look a little complicated at first glance, but actually there’s very little going on. As you’d expect, we want to control the LED light using PWM-enabled pins (to have fine-grained control of the brightness) and the potentiometers (pots) are being read by the analogue pins.

Populate the breadboard
Populate the breadboard

Step 03

Connect the Arduino and Raspberry Pi

Assuming you don’t plan to write up the code immediately yourself, you can grab it from the disc or from the website and drop it in your home folder. With the USB cable from the Arduino plugged into the Raspberry Pi, you simply need to
run the code with the following:

$ python

Adjust the pots for the corresponding colour of the LED and the colours should change. If the pots are adjusting the wrong colours, just swap them over. You could use a table-tennis ball or plastic mug to diffuse the light to great effect.

Step 04 Setting up the pins

As we demonstrated in the last project, it’s easy to name and set the Arduino pins with Nanpy – in our code we’ve used two simple for loops to do the job. The debug value below simple prints the values of each pot to the terminal – very useful for debugging or getting a better handle on the code.

Step 05 Functional operation

There are only really three main functions here, written with self-explanatory names. Firstly, get_pots() reads in the analogue pin value associated with each pot-pin and returns a tuple of the value for red, green and blue respectively. This is used by the colour_mixing() function to assign values to each of the associated PWM pins to change the colours of the LED.

Step 06 Keeping things running

The main() function is where the other functions are set to work. Inside the function, we’re asking Python to mix the colours (and print the values if debug is True) forever, except if we press Ctrl+C – initiating the keyboard interrupt. Since we want to clean up after ourselves, this action with trigger close_pins() – this turns off the pins attached to the LED, ready to be used next time.