Starting Out With A DesignSpark Pmod HAT
When I saw that Andrew Back had put together a Python library for the DesignSpark Pmod HAT, I knew I had to get my Raspberry Pi out of its draw and give this HAT a try.
Digilent’s Pmods (Peripheral modules) are pretty well known to most microcontroller development board users – for example, you will find Pmod headers on Cypress PSoC boards – as they are great for rapidly prototyping your ideas. There are a huge number of modules that allow you to quickly do everything from analogue/digital conversion to adding a keypad or SD card to your system using 6 or 12 pin headers: so no fiddling with a breadboard and wires. Bringing this resource to the Raspberry Pi environment is a boon to anyone who wants their Pi to interact with the world.
I already have a few Pmods, one of which is the OLEDrgb (an Organic RGB LED module with a 96×64 pixel display) that is supported by Andrew’s Python library, so I figured this should be a great test of quite how easy it is to get up and running with this great new, low-cost prototyping tool.
Out of the Box
The Pmod HAT comes in a pretty cute little box:
…and is well protected by the packaging inside:
The board comes fitted with metal stand-offs that include the fixing screws, so you don’t need any other hardware to securely fit the module to your Pi:
As you would expect, the module conforms to the HAT spec, making its dimensions 65mm x 56.5mm and it is compatible with all Raspberry Pi boards which use the 40-pin GPIO connector, including the Model A+, Model B+, 2 B, 3 B, 3 B+, Zero W and Zero.
There are 3 (12-pin) Pmod ports: two support SPI (JA/JB), one supports I2C (JB) and one supports UART (JC). All three ports support GPIO.
There is also a barrel jack for power, which is capable of powering the Raspberry Pi (if your power supply can deliver at least 1.3A) though I haven’t tried this myself. A word of caution though – don’t try to power this barrel jack and your R-Pi power input at the same time, unless you like the smell of smoking electronic components.
You will probably also have noticed some jumpers on the board. When shorted, JP1 and JP2 enable pull-up resistors for the I2C pins on port JB. The other jumper (JP3) write-enables the on-board EPROM which contains the device tree fragment and other info for the R-Pi OS and drivers. Unless you are really hardcore, you are not generally going to need to change the contents of this EPROM.
From Zero to I/O Hero in No Time
First, you will have to install the libraries. I won’t re-hash this process as it was also covered thoroughly in Andrew Back’s original article and both sets of instructions are very easy to follow, even if you have limited Raspberry Pi experience.
The basic example for the OLEDrgb is a true ‘Hello World’ example. As we are getting the words onto the LED module display and putting them in a bounding box, the Python code is a little more complex than just:
but, thanks to the library calls, it isn’t that much more complex:
#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (c) 2017 RS Components Ltd # SPDX-License-Identifier: MIT License """ Display Hello, World! in bounding box. """ from DesignSpark.Pmod.HAT import createPmod from luma.core.render import canvas from luma.oled.device import ssd1331 if __name__ == '__main__': try: oled = createPmod('OLEDrgb','JA') device = oled.getDevice() with canvas(device) as draw: draw.rectangle(device.bounding_box, outline="white", fill="black") draw.text((16,20), "Hello, World!", fill="white") while True: pass except KeyboardInterrupt: pass finally: oled.cleanup()
If you are new to Linux, you may not realise that once you have used a text editor to save your Python program (for example, as: hello.py), you have a couple of options when it comes to running it from the command line:
You can directly invoke the Python interpreter to run the program with:
$ python hello.py
Or you can make your program executable:
$ sudo chmod +x hello.py
You can then run it by using:
$ sudo ./hello.py
For this to work however, your first line must include:
This is known as the ‘shebang’, and tells the operating system where it can find the interpreter for the script that follows.
If you really want, you can run the program from IDLE too – it’s up to you. However you start it, you should see the words “Hello World” inside a white bounding box on your OLED screen:
Once you have done this, you really are just a minute or two away from running the more advanced examples such as the analogue clock example and the Conway’s Game of Life simulation:
Game of Life
The beauty of this is that it took virtually no hardware set-up time to run these programs and if I want to do something else tomorrow, for example: measuring temperature, it is just a matter of swapping the Pmod and I am away.
Andrew Back’s library is a great starting point for a project as you can quickly and easily validate your hardware with the examples before using those examples as a jumping-off point for whatever it is you want to try out for yourself. Getting to grips with LED screens can be a bit daunting at first, but the examples really do give you what you need to get some pretty sophisticated screen displays up in record time.
Altogether, the Pmod HAT and Andrew’s library make a powerful ‘quickstart’ kit for your next project by allowing you to quickly (and cheaply) try things out, before settling on a hardware combination that you want to move forward with.
My verdict: two thumbs up!
CommentsAdd a comment
Hi, Thank you for your nice article.
If you missed command line editor tool here:
"to running it from the command line:",
do you mean "vim, emacs, nano, etc" ?
If it will be that, please tell me, and add in this article.
@teshiba - Thanks for your question. When I talk about running a program or script from the command line, I mean from the prompt you find in a terminal window - or indeed the prompt that is available to a Linux user who isn't running a windowing desktop. For example, if you are running Ubuntu 18.04, you can click on the 'show applications' icon (the 3 x 3 grid of dots at the bottom left on the task bar when it is in its standard position) and go to the second page of applications. There you will find the application called 'Terminal'. You can run this application to enter commands from the ‘command line’ – which is simply the line where the $ prompt is in the terminal. In pretty much every Linux distribution there will be a terminal application, though some have 'improved' versions which may have another name. I hope this helps.
Sorry, meant to also say: I tend to use nano from the command line to edit with, but you could equaly use the graphical text editor that comes with your distro or even a programming specific editor like Notepadqq or Geany.