Callum Kirkwood

Blog
Projects
Photography
LinkedIn
GitHub
Twitter

potHat

potHat

Custom Raspberry Pi HAT with an ADC, 3 potentiometers and LEDs

Built as a solution to quickly add analogue inputs to the Raspberry Pi without the hassle of setting up a breadboard each time. The proto HAT also breaks out the Pi’s GPIO, so extra-long headers can be used to attach Pimoroni’s Blinkt or Mini Black Hat Hacker.

With a few spare rows on the proto HAT, I added a trio of LEDs so that I could work with relatively simple scripts to get my Python up to speed. The pots and switch can be used to alter values of the outputs like the colour/brightness/blink speed of LEDs, or can just as easily connect with anything else attached to the available GPIO pins.

It’s a relatively simple circuit ported straight from a breadboard; Adafruit’s MCP3008 tutorial covers the whole setup. There isn’t too much room left on the proto HAT once the pots and ADC are soldered on, so the positioning is fairly restrictive. I also mis-counted the number of spare rows, thinking I only had room for 2 LEDs - I added a third yellow LED when I spotted the last two remaining rows, but it does spoil the traffic light visual.

A few lumps of Sugru (or stand-offs) also help to secure the HAT and the pots.

Wiring

Hardware
Optional

potHat with Blinkt

GitHub Repo

The examples below can be found in full in this repository, along with a few other experiments.

Setup

Adafruit’s tutorial covers the library install and initial setup. After that, the GPIO pins just need to be defined at the start of a new Python script:

import Adafruit_MCP3008

CLK = 11
MISO = 9
MOSI = 10
CS = 8
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)

The main inspiration for building this was to control the RGB values of a Blinkt stick, which turned out to be straightforward enough once I’d found a formula to adapt the range of the ADC outputs from 0-1023 to 0-255.

import blinkt
import time

## Convert 10-bit ADC output to RGB friendly 0-255 and store as variables
## NewValue = (((OldValue - OldMin) * (NewMax - NewMin) / (OldMax - OldMin)) + NewMin

while True:
	r = (((mcp.read_adc(0) - 0) * (255 - 0)) / (1023 - 0)) + 0
	g  = (((mcp.read_adc(1) - 0) * (255 - 0)) / (1023 - 0)) + 0
	b = (((mcp.read_adc(2) - 0) * (255 - 0)) / (1023 - 0)) + 0
	blinkt.clear()
	blinkt.set_all(r, g, b)
	blinkt.show()
	time.sleep(0.1)

pot_blinkt.py

Alternatively, the active LED can be moved from left to right with a pot, which could make a nice menu/selection interface:

while True:
	r = (((mcp.read_adc(0) - 0) * (255 - 0)) / (1023 - 0)) + 0
	g  = (((mcp.read_adc(1) - 0) * (255 - 0)) / (1023 - 0)) + 0
	scan = (((mcp.read_adc(2) - 0) * (7 - 0)) / (1023 - 0)) + 0
	blinkt.clear()
	blinkt.set_pixel(scan, r, g, 0)
	blinkt.show()
	time.sleep(0.1)

Finally, kickstarting my experiments with homemade IoT (more on that to come), I managed to push the outputs to Dweet.io, which can then be retrieved to control other devices/services. I’ll cover Dweet in a bit more detail in later posts, but their website has enough detail to get up and running in minutes - dweepy is also a handy Python wrapper for the service.

import dweepy
import Adafruit_MCP3008
import time

## Software SPI setup
CLK = 11
MISO = 9
MOSI = 10
CS = 8
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)

while True:
	red = (((mcp.read_adc(0) - 0) * (255 - 0)) / (1023 - 0)) + 0
	blue  = (((mcp.read_adc(2) - 0) * (255 - 0)) / (1023 - 0)) + 0
	green = (((mcp.read_adc(1) - 0) * (255 - 0)) / (1023 - 0)) + 0
	dweepy.dweet_for('pot-one', {'red': red})
	time.sleep(0.1)
	dweepy.dweet_for('pot-two', {'blue': blue})
	time.sleep(0.1)
	dweepy.dweet_for('pot-three', {'green': green})
	time.sleep(0.2)
What’s Next?

The HAT also works really nicely with gpiozero, although I prefer the range on the ADC outputs that the Adafruit library provides.

The configurations between inputs and outputs are pretty much limitless, so this should be a really good tool to build confidence with simple scripts and hopefully develop some simple workshop exercises.

When I run out of new projects, I’ll probably build a new version - I’d replace the LEDs in the correct order, and take a bit more care with the soldering…