Linux Format

Analogue explained

Les Pounder shows us how we can use analogue sensors and inputs with our Raspberry Pi to control Neopixels.

-

Les Pounder turns it up to eleven connecting his analogue thingies to his GPIO whatsits.

All models of Raspberry Pi come with the GPIO, the pins that enable the use of electronic components and add-on boards. But no model of Raspberry Pi can interact with analogue components, as the Pi does not come with an Analogue to Digital Converter (ADC). Step forward the MCP3008 ADC. In this tutorial we shall use it with three potentiome­ters to control a neopixel ring.

Hardware setup

Please refer to the wiring diagram (see You Will Need, below) for this tutorial, there are quite a few connection­s to be made.

We start the hardware build by inserting the MCP3008 into our breadboard so that it is over the central channel. The notch on the MCP3008 should be facing the top of the breadboard. The MCP3008 has multiple connection­s to the Raspberry Pi, for power and for a hardware connection to the SPI bus. These connection­s are made on the one side of the chip (pins 9 to 16 according to the datasheet). Pins 1 to 8 are reserved for the eight channels that are available for us to use with analogue devices.

Potentiome­ters, sometimes referred to as “pots”, are three-pinned variable resistors. By turning these pots we can vary the voltage output from the centre pin. Potentiome­ters’ three pins are voltage, output and ground. They can come in many forms, but we’re using single-turn potentiome­ters similar to those used as volume controls in amplifiers. Others include linear potentiome­ters, such as those used on a mixing desk, and there also “trimpots” used on circuit boards where infrequent adjustment­s are needed.

Neopixels are a brand name, created by Adafruit, for the WS2811/12 series LEDs. Each pixel in a series can be individual­ly controlled: its colour, brightness and whether it is on or off. Neopixels need precise timing to control them, and to do that we need to use a GPIO pin on our Pi that can be used with Pulse Width Modulation, PWM. From experience we know that pin 18 can provide this. To power the Neopixels we can also use the 3V and GND pins, which have been broken out to the breadboard as per the diagram.

Software setup

No matter what version of Raspberry Pi you are using, even the Pi Zero, our first task is to turn off the audio output as this will interfere with our Neopixels. To do this we need to alter the config.txt file in the boot directory, so open a terminal and type the following. $ sudo nano /boot/config.txt

At the end of the file, on a new blank line, add the following two lines to comment the change for later reference, and to turn off the 3.5mm audio jack on your Raspberry Pi. #For Neopixels. Forces audio via HDMI and turns off 3.5mm jack. hdmi_force_hotplug=1

To save and exit the editor, press Ctrl + O and then Enter, then press Ctrl + X. Reboot your Pi to enact the change.

Once rebooted and back to the desktop, we can install the Python library that will enable us to control the Neopixels. In a terminal type the following: $ sudo pip3 install rpi_ws281x

Newspapers in English

Newspapers from Australia