I recently posted a tutorial on using an HD44780 LCD (with I2C backpack) with the Raspberry Pi. I mentioned at the time that I had also been looking at small enclosures for the Pi in the hope of making something. Well, in a few hours of spare time I had in between bouts of Pi Wars admin, I dug out my soldering iron, rotary tool and various other oddments and made something I’m calling palmPi.
Bill of materials
- 1 x Raspberry Pi Zero W
I wanted the Pi to be able to connect to wifi and display it’s IP address so I could ssh into it to alter the code - 1 x HD44780 LCD with I2C backpack
I’d already done the work to figure out how to use it, so it made sense to use one of these. - 1 x BMP280 sensor
This has a temperature sensor and a pressure sensor on-board - 1 x ADS1115/1015 I2C analog-to-digital converter board
I don’t know why, but I just love using analog sensors. I thought about adding other sensors but I ran out of space inside the case - 1 x TMP36 (analog) temperature sensor
Cheap-as-chips sensor, very small - 1 x ABS enclosure from Rapid Electronics
These are blummin’ lovely enclosures from Rapid – they fit in the hand nicely and are very sturdy - 1 x red illuminated, momentary push button
Because everyone loves a good LED 🙂 - 1 x toggle switch (any one with two pins will do, these are quite nice, but I went for something smaller but can’t remember where I got it!)
I needed something to hard-cut the power. I had these knocking around and had previously used one in another handheld device. - 1 x Adafruit perma-proto HAT board (other prototyping boards are available, but this was just what I had to hand!)
These perma-proto boards are terrific for creating something HAT-like and have handy power rails and GPIO breakout pins - 1 x Adafruit PowerBoost 1000C
This is an impressive piece of kit. It can draw power from a LiPo and also re-charge it as it has a microUSB port built in - 1 x 2000mAh 3.7V LiPo battery
These are as safe as you can get when it comes to LiPos. They are also very flat which means they’re ideal for stacking beneath things in tight spaces - 1 x Adafruit Micro LiPo charger
I wanted to pre-charge the battery so grabbed one of these - Narrow-gauge heat-shrink tubing
Just to give some of the connections a bit of extra security - Various coloured wires to hook everything up
Tools used
- Soldering iron (the link is the one I actually used), solder, wire sponge, the usual.
- Rotary tool (Dremel or other type)
- Screwdriver (because the ABS case screws together)
- Side cutters (okay, okay, I used a sharp pair of scissors, but I looked for my cutters and couldn’t find them!)
- Wire strippers/cutters
Construction – Hardware
Preparation
First of all, I soldered a 40-pin male header onto the Raspberry Pi Zero W and a female header onto the perma proto board. This means I could connect the proto board to the Pi Zero. I could have soldered everything together permanently first of all, but I wanted to use a Raspberry Pi 3 to do the installs and programming on to start with as it’s a lot quicker than the Pi Zero. I also (very loosely) placed all the components inside the case to make sure they’d fit in the space.
LCD
The first thing I did was to make sure that I could get some output on the screen. I used my own tutorial for the HD44780 and wired up jumper cables to the pins, bending them down slightly to get the right angle.
I managed to lose the little jumper that enabled the LCD back light (doh!) so I just plugged an extra F-F jumper cable in to connect the two pins. I cut the ends off and stripped the four main jumper wires and then tinned the stranded ends to make them easier to work with. This requires a fair amount of skill because you don’t want to make them too thick otherwise they won’t go through the proto board. Having established (by plugging directly into the Pi) that the screen worked, I soldered the LCD wires to the proto board so that it was more permanent. I hot-glued the screen into the pre-cut hole in the enclosure. It’s not perfect, but it will do for me. The alternative was to get Tim to laser-cut me something that would fit snuggly but what with Pi Wars, I didn’t want to add to his workload!
Button
I knew that, at the very least, I wanted a button to shutdown the Pi safely. I looked through my (large) collection of buttons and decided to use an illuminated button (obviously because I wanted the extra soldering practice..!)
I soldered four wires to the bottom of the button and then heat-shrinked the terminals. I then soldered the wires onto the perma proto board – I used two GPIO pins, one for the LED so I could blink it and one to read the actual button press. Don’t do what I did and forget to put the washer back on beforehand otherwise you’ll have to do what I did and hot-glue the button in place!) At this point, I realised I should have soldered an inline resistor for the LED, but knew from past experience that it wasn’t the end of the world (he says, hoping that’s true). If you’re doing this yourself, use 220 ohms, or similar, on the ground lead.
On/off switch
The whole project is run off a LiPo battery connected via a PowerBoost 1000C which is able to supply 5V at 1A, just enough for a Pi Zero W. One of the nice things about the 1000C is that it has pins that disconnect the battery entirely from your Pi and other hardware. You can read about all those pins over on the Adafruit site. So, I soldered wires to the switch, heat-shrinked them and then soldered the other ends to the PowerBoost. The other nice thing is that the board has an LED status indicator that lights up if the output power is switched on. So, I was able to test out the switch independently of booting up the Pi. I then got the rotary tool out and drilled holes for the button and the switch. I’ve decided my rotary tool skills need… improving… There’s a bit of air around the illuminated button. Oops!
BMP280
The BMP280 is a nice little breakout board which lets you have the sensor output over I2C. Again, I soldered wires to the breakout board, drilled a hole with the rotary tool, passed them through and soldered them onto the perma proto. Taking the do-a-little, test-a-little approach, I made sure that the sensor was being picked up:
i2cdetect -y 1
…and that sensor readings were coming through to the Pi using code (of which more later).
Analog
To read from the TMP36 sensor, I needed to add an analog-to-digital converter. I had some ADS1115/1015 boards left over from another project and these give you four analog channels. I soldered the ADS directly onto the perma proto board using pass-through headers to save on vertical space inside the case.
I then took the TMP36 sensor, looked up the pin-outs again (sigh) and soldered it to channel A0 on the perma proto board, 3v3 and GND.
Once again, I checked to make sure I could read the output and then realised that I needed to work out the conversion from the ADS reading to the actual temperature. (Eurgh! Maths!) That nifty bit of manipulation was contained in a function:
adc = Adafruit_ADS1x15.ADS1015() def read_internal_temperature(): ADC_GAIN = 2 adc_reading = adc.read_adc(0, gain=ADC_GAIN) adc_mv = ((adc_reading * 2.048)/2048) * 1000 temp_c = (adc_mv - 500) / 10 temp_f = (temp_c * 1.8) + 32 return "{:+.2f}".format(temp_c), "{:+.2f}".format(temp_f)
Power
As well as the on/off switch enable pins, I also soldered the 5V and GND pins from the PowerBoost to the 5V and GND rails on the perma proto, thus powering the Pi from the battery. Up until then, I had powered the Pi (and everything else) from a normal PSU. The nice thing about the on/off switch at this point is that you can hook the battery up, but leave it toggled off using the on/off switch and still power the Pi and sensors etc from the mains. Always consider whether running off the battery is a good idea at any point – you don’t want the battery to run down and for the Pi to crash in the middle of editing your code! If you’re running a Pi 3 off one of these batteries, you will not only get under-power warnings but the battery won’t last for very long.
I’ve just realised I forgot to wire up the low-battery-warning pin to a GPIO – I’ll take it apart later and do that!
After getting everything soldered up and finished, I then rotary cut another hole in the case for a power cable and then hot-glued the PowerBoost to the case. I was quite chuffed – everything is attached to the top half of the case, meaning that I can take the bottom on and off without pulling any wires out! Here’s a picture of the PowerBoost (it’s that shadowed blue bit in the centre, at the bottom) in it’s final place beneath the main perma proto and the Pi Zero W.
And here is a final picture of the innards. The LCD is on the left and you can see the on/off toggle switch at the bottom, the illuminated button at the top and then the perma proto (with hidden Pi Zero underneath) on the right. The bit of electrical tape on the proto is where I’ve temporarily attached the battery to the proto board for storage.
Software
The main structure of the program is similar to that which I wrote for the Picorder but it’s a lot simpler because I’m using fewer sensors. It sets everything up then loops through, getting readings from the sensors and outputting them to the LCD.
To read the BMP280, I pulled in some code from Matt Hawkins (Raspberry Pi Spy). It’s a great mini-library which reads all the sensor outputs and returns them from a function. You can find out how it works over on Matt’s blog. I carried out a little bit of updating to get it to Python 3-standards.
To read the ADS1115 analog values, I installed and used an Adafruit library which is handily documented over on their learning platform.
I decided at this point that I wanted to log the sensor readings to an online platform. I chose Initial State. I’ve been following them for a couple of years and it’s a very mature platform. It’s also free to use up to a point and would suit my purposes. My code sets up a new ‘thread’ (a separate process, sort of) to take and log the readings to Initial State. This is a very simple implementation and doesn’t do much more than log the readings and then wait for 15 minutes before logging again. You can see one of the graphs it produces below:
And you can see the configurable dashboard with all the readings on below:
All of my code is available here on GitHub.
Yes, I have left my Initial State bucket credentials in there! I’m not paying for my plan and you can’t see the output anyway. Normally, you wouldn’t do this, you’d put them in a config file and set-up a .gitignore file to not commit those.
Update: I’ve now added code that tweets the startup, sensor readings and shutdown. For this, I followed the instructions on this page for the Twython library.
See it in action
I’m sure by now you’d like to actually see the palmPi in all its glory! Or maybe not, but anyway, here goes!