Pymon – A Simon clone for the Raspberry Pi

Recently CPC had a great time at Makerfaire UK and we took along a few games for the public to play. One of those games was a clone of the popular 80s game SIMON.

For those a little too young to remember. SIMON was a memory game that challenged the player to remember a sequence of lights that were underneath four coloured buttons around the perimeter of the game. The game increased in difficulty and speed as you progressed through the levels, before finally beating you…that part was inevitable.

Hardware Build

For this project you will need

For our version of the game we thought “out of the box”, as hackers / makers a little problem solving and sideways thinking is part of our workflow. So what did we use? Firstly we needed a box to contain the project and we needed a BIG box, so a 50L Really Useful Box was called for.

 

To power our RGB LEDs we needed a powerful power supply and for that we used a 5v 4A supply.

We then connected the barrel of the power supply to a female barrel jack connector which then broke out the connections to two screw terminals, + and GND.

What we needed now were our inputs, and we needed a robust solution, and that is where Kitronik’s range of arcade buttons came in. These buttons act as a momentary switch that are triggered when pressed firmly. Yellow, Pink, Green, Blue

With our inputs complete, now we need to work on our outputs, the SIMON game used just four LEDs, so we needed to go big! With 150 RGB LED! These RGB LED are super bright, in fact we had to reduce their output to save our eyes!

With our inputs and outputs selected we needed to find a platform that we could code to tie the project together, and that platform had to be the new Raspberry Pi 3.

We started our hardware build by laying out the RGB LED strip to ensure that it comfortably fit inside the box. With a little insulation tape we created a snaking strip of light ready for testing. We connected the strip to our Raspberry Pi, specifically GPIO18. We then connected the GND of the strip and the GND of our power supply to the Raspberry Pi GND pin using a male to female jumper wire and a terminal block to hold them together. We now had a common ground (GND). The + from our power supply was connected to the + of our LED strip using terminal block. With our LED now powered and connected to the Raspberry Pi we now move on to creating our method of control, using our arcade buttons.

Arcade buttons are relatively simple to work with. They have no polarity, meaning that there is no specific way to wire them up. We like to be consistent so chose one pin on the button that would act as ground and use that pin across the buttons. With this choice made we cut lengths of single core wire to length, we used black wire to identify our GND wire. To hold the wire in place we used a soldering iron and solder. Next we cut more coloured wire to length and soldered that to the arcade buttons. We now had four buttons soldered and ready for use, but how do we connect them to the Raspberry Pi?

The humble breadboard, used for decades was the piece of our puzzle that brought everything together. Using a breadboard and a terminal strip we connected the buttons of our game to their corresponding GPIO pins.

The complete diagram illustrates the electrical connections that we made.

The breadboard diagram for Pymon
The breadboard diagram for Pymon

We then plugged in our Raspberry Pi and started to write some code.

Software Build

We used the Raspberry Pi 3 and its Raspbian operating system as there is so much support out there. We built the code for our project in Python but before we started to code we needed to install a few extra libraries for working with our RGB LEDs so here is how we did it.

To install the software you will need to connect your Raspberry Pi to the internet.  Open a terminal, you can find the icon for which in the top left of the desktop.

In the terminal type the following to update the list of software that can be installed on your Raspberry Pi.

With the list up to date we now download the software and install.

This may take a little while to complete, depending on the speed of your internet connection and Raspberry Pi.

Next we download a copy of the source code that enables us to work with the  RGB LEDs from the Pi. We use git clone to download the latest version of the code, then we change our directory so that we are inside the directory that we have just downloaded. Lastly we run scons to compile the code ready for installation.

To install the code we now need to change directory to the python directory and then run a Python script to install the library.

After a few minutes the library is installed and we are ready to start coding.

Python Code

All of the code for this project can be downloaded via this link

To code in Python we need a Python editor, and we can find this in the main menu, under Programming,at the top left of the screen. Controversially we are using the Python 2 editor as the library we are using is written especially for this version. When the Python editor opens we will need to click on File » New File to create a new window. For ease we would recommend saving the blank document now with the filename pimon.py. Subsequent saves will now be a lot quicker.

We start the project by importing four libraries, we import all of the neopixel library using * so that we do not need to reference the neopixel library when calling classes / functions from it. On the next line we import the Button class from the new GPIO Zero library, a library designed to enable anyone to hack with the Raspberry Pi GPIO. From the time library we import the sleep function, used to control the pace of our project. Lastly we import the random library, a collection of classes and functions that we can use to add a random element of chance to our game.

Next we use four variables to identify the colour of the buttons attached to a particular GPIO pin. Here we use the GPIO Zero Button class to identify that it is a button attached to a certain pin. For example we have attached our Green arcade button to GPIO pin 4.

We now configure the variables for our RGB LEDs, for starters we set the number of LEDs in the strip, in this case 150. We earlier attached our RGB LEDs to GPIO18 of our Raspberry Pi and now we instruct the code to find the connection. We next set the frequency at which we control the LED, in this case 800khz. We next set the DMA channel used to generate the signal. Next we set the brightness of the LED. RGB LEDs are really bright, so as a matter of safety it is prudent to limit their brightness to 50%, which is 128. 100% brightness is 255. If we need to invert the signal for NPN transistors then we set the invert to True, but in this case we use False.

We now create a function to control the RGB LEDs, this function called “neo”. This function has two arguments, the color of the LEDs and the time that it should on for. The function contains a series of if..elseif conditional tests. These tests check the colour passed by the user as an argument and if the colour is identified then the LEDs are updated to show that colour. We use a for loop to update the LEDs one by one, once each LED is updated we need to instruct the strip to show the changes made. We then sleep for however long (n) may be. Then we turn off each LED using a similar for loop construct. Here we see all of the code for every colour.

Next we create another function that will flash all of the LEDs green if the correct sequence is input by the user.

We also create a function to flash all of the LEDs red.

With our functions created we now move on to the main body of code that will control the logic for our game.

We start by setting up the strip of RGB LEDs and passing the information on the strip to a variable. We then initialise the strip, strip.begin() before using a try, except construction to handle any errors. Here we create a list and store the colours used for the lights.

Next we create a while True loop that will constantly check to see if the green button has been pressed. This event is a blocker and will prevent the code from progressing further until the button has been pressed.

Once the button has been pressed we enter into a for loop that will iterate 16 times. The loop will flash the colours contained in the “lights” list for a tenth of a second.

When the for loop ends the script randomly shuffles the contents of the “lights” list and prints the contents to the screen, which we can see in the Python shell. The code sleeps for half a second before creating a new blank list called player, which will store the button presses of the player.

We next play the new randomised light sequence back to the user, the pace is rather quick at 0.3 of second per flash, but this can be changed to suit the player. We then use a for loop to reset the strip so that every LED is off.

We now create a while loop and this loop will test the length of the player list. While the list is less than four items in length it will check for a button press and then add that press to the player list, thus giving us the players choice of colours. The colour choice is repeated to the player as a means of feedback, indicating that their choice has been stored. We also print the choice to the Python shell for debug purposes.

When the player has finished entering their choices, the code will play their choices back to them. If those choices matched the original randomised choices then the correct function is called and the game will flash all of the LEDs green 9 times. But if the player choices are incorrect then the wrong function is called and the LEDs will flash red 9 times.

Our last section of code handles any errors and enables us to exit out of the project by pressing CTRL + C, should the need arise. By exiting we turn off all of the RGB LEDs and the game stops.

With our code and hardware complete we are now ready to play a simple game of Pymon.

Here is a complete code listing to use as a reference.

Getting ready for take off with Astro Pi

The Astro Pi project has seen two Raspberry Pi (model B+) computers fly up to the International Space Station (ISS) with ESA (European Space Agency) astronaut Tim Peake.

At the heart of the project is a board called the Sense HAT which is a scientific platform for exploration and experimentation. The Sense HAT comes with a series of sensors for use in your experiments. For example there is an accelerometer to measure G-force and acceleration, a magnetometer which can measure magnetic forces and be used as a compass. There is also a gyroscope to measure orientation and a temperature sensor which can measure the temperature and humidity of an area. Lastly we have a barometer which can be used to measure atmospheric pressure. These sensors are packed onto the Sense HAT along with a five way joystick, for input and an 8 x 8 grid of red, green, blue (RGB) light emitting diodes (LEDs) which can be used for basic data output.

The Sense HAT is a powerful platform for experimentation and that is why two of these boards are also present on the ISS, inside a very special enclosure.

The Astro Pi Flight Case has been carefully designed to meet the ESA’s exacting criteria. Made from a single block of aluminium the Astro Pi flight case is designed to safely enclose the Raspberry Pi, Sense HAT and Raspberry Pi Camera. The aluminium also acts as a heat sink to keep the devices cool.

The flight case is a thing of beauty, but alas it is rather expensive, so what if we could make our own case?

Thanks to Ryanteck, who offers a great on-demand 3D printing service, we had a case 3D printed and posted to our office makerspace where our maker in residence set to work.

Ryan has done an excellent job with the 3D printed parts and each part required only a small amount of preparation before being assembled. We used the official Raspberry Pi Foundation worksheets as our guide to assembling the parts ready for construction.

We built the unit to the exact specifications given in the guide, this included sourcing an 11mm male to female spacer. The tolerances inside of the case are exacting and require this level of attention to detail.

We have a stock of the APEM switches used for input on the flight case, so we used a set of these for our “Hero” unit, a term used to describe a show piece / prop which has an extensive level of detail. These switches are exquisite and are extremely robust, they have to be to survive on the ISS for an extended period.

We also tested a series of compatible switches and found that we have a more cost effective and similarly robust switch in stock.

SW03233-40

We found these switches to be a great alternative to the APEM switches.

Work is still ongoing with our case, we need to work on a way to route the seven wires that are used for our switches, to reduce the chance of a short and ensure a good connection. These wires are broken out from the lower group of eight GPIO pins using a GPIO extension. Hopes are high that we can use an old 26 pin GPIO extension and then directly connect to these lower GPIO pins.

This is a great project to undertake and the team are very excited to complete it.

Value for Money Raspberry Pi Setup

Having had quite a lot of spare time on my hands recently, I’ve been checking out some of the recent posts in social media regarding the Raspberry Pi. One of the things I’ve noticed is a number of articles questioning whether or not the Raspberry Pi really is, as is commonly claimed, a cheap (sub £35) computer. Many of these articles attempt to effectively debunk this claim by arguing that because of all the extra bits and pieces required (e.g. mouse, keyboard, monitor/TV, HDMI cable, micro SD card, power supply, WiFi dongle/Ethernet cable, etc.) you effectively have to spend a lot more than the £35 price tag of the Raspberry Pi in order to get it working.

Now I’ve never been one to get all political, or for jumping on soap boxes, but I do genuinely believe that it is possible to get your Raspberry Pi up and running for very little more than the basic price of the Pi itself. If you don’t believe me, read on.

Continue reading Value for Money Raspberry Pi Setup

Portable Raspberry Pi

Taking your Raspberry Pi on the road can be quite involved.

Normally we need a keyboard, mouse, power and a screen. Some of these components can be easily packed ready for hacking on the move. But some, namely the power and screen, require us to compromise in some way. But can we create a truly portable Raspberry Pi kit?

Kev and Rachel do a lot of travelling and they needed a portable kit that could be taken into meetings and used to demonstrate the power of the Raspberry Pi.

So my brief was simple

“Create a portable Raspberry Pi kit.”

 

Based on this brief I created my own requirements, having used the Pi “in the field” and understanding their needs.

The kit should be

  • Based on the latest Raspberry Pi
  • Be sturdy for transport
  • Use a common power source (micro USB)
  • Have access to the GPIO for add on boards

So what did I use?

We started by building the HDMIPi screen kit. This is a really robust and well thought out kit that provides an excellent 1280 x 800 pixel display inside a series of LASER cut acrylic layers.

The use of the acrylic layers enables the screen to be sandwiched between the layers, providing a tough shield for the delicate screen.

HDMIPi also comes with its own controller board that is used to route the HDMI output from our Raspberry Pi to the screen, but this board also provides a single point of input for a power supply, in this case the Tecknet IEP1500 power bank. The power bank provides 15000mAh of power and in our tests we saw approximately 5 hours of use from it.

We used a Raspberry Pi 3 in our build, ensuring that we have the latest version of this popular single board computer which is now powered by a quad code processor running at 1.2GHz! The Raspberry Pi 3 also comes with built in WIFI and Bluetooth, enabling our dynamic duo to have Internet access, but without taking up a USB port on the Pi.

The Raspberry Pi 3 is also cleverly hidden inside of the HDMIPi case, protecting it during long journeys, but with one slight problem, namely the GPIO pins are now behind the screen and not really within easy reach.

But we needn’t have worried as the Pirates of Sheffield, aka Pimoroni have created their own expansion board called the Black HAT Hacker. This board breaks out the GPIO pins to a PCB plate that connects to the GPIO via a ribbon cable. The Black HAT Hacker enables easy access to all of the GPIO pins and it can be used with other add on boards, such as the Astro Pi project’s Sense HAT. This will give the dynamic duo all of the flexibility that they need to “hack on the go!”

With the kit assembled our next goal is to create a suite of demonstration applications that will demonstrate the power of the Raspberry Pi. More on that in a future post.

So what compromises did we make?

Honestly? Not really that many.

The HDMIPi screen gives us 1280×800 resolution, plenty for hacking.

Our power supply gives around 5 hours of continuous use (under stress test)

To use the GPIO we had to use the Black HAT Hacker, a compromise yes but a great product that can be used to investigate the behavior and debug your favorite add on boards.

We did have to compromise on a wired keyboard and mouse, due to a few issues with the still buggy Bluetooth card used on the Raspberry Pi 3 preventing the use of our Rapoo E6700, for now at least.

So now we have the kit to help our Dynamic Duo in their travels across the UK!

micro:kit – A low cost breadboarding solution

With the micro:bit finally making its way into our schools and nearly into the hands of young makers around the United Kingdom, we are now starting to see a few teething problems from educators.

The BBC micro:bit
The BBC micro:bit

One issue that sprang to the foreground involved shorting the GPIO pins which can cause the micro:bit to get rather warm. In normal operation the micro:bit runs absolutely fine and has no issues, but in this particular case the educator had tried to create a traffic light simulator with three LED, red, yellow and green, all of which we directly used with the board, thus causing the LED to short each other out.

Normally the micro:bit is used via crocodile clips that connect to the pins at the bottom of the board. But using crocodile clips with components as small as an LED can be quite troublesome.

So the problem we faced was

How can I use the micro:bit with a breadboard to prevent shorts from happening?

 

In order to properly create a circuit we would need to use something that could secure the components yet still be electrically conductive. So with a little ingenuity and a lot of prototyping we found an ideal solution.

For this project you will need.

M4 Countersunk Machine Screw 12MM (FNCP4M12)
Nylon M4 Nut (FN00710)
M4 Nylon Washers (FN00702)
Solid core hookup wire (MK00170)
A red LED (SC11575)

Step 1: Trimming the wire

We will need to cut and strip 5 wires which we will use to connect the micro:bit to a breadboard.

Typical colour coding for wiring is to use red to indicate a live voltage, in this case 3V. Black is used to indicate a Ground wire, often referred to as GND.

For wires 0,1,2 pick any colours that you wish.

Using a wire cutter, cut around 15cm of wire for each colour.

Using a wire stripper, strip 1cm of wire from one end, and then strip approx 3cm of wire from the other end.

Step 2: Building the kit

Take 1 of the countersunk machine screws and insert it into 3V.

Place a nylon washer on the machine screw thread.

Take the wire and wrap it around the machine screw, ensure that it wraps around the screw multiple times and that it is not in contact with the micro:bit.

Take a nut and secure it to the machine screw so that it is firmly in place. Do not use any tools for this step as you may damage the micro:bit.

Repeat this step for the remaining pins.

Once complete it should look similar to this.

Step 3: Attaching the breadboard

For this step we will need

A breadboard
A red LED

When working with hardware and software it is always prudent to test the hardware before you we write any code. Identifying that the hardware is working enables us to quickly move on to writing code that will control the hardware.

To test our hardware we shall use a breadboard which is a semi permanent method of building a circuit. A breadboard has a series of holes arranged into columns and rows.

An LED is a Light Emitting Diode, and when powered it lights up. An LED has two legs, a long leg which is called the Anode, this leg receives power. The shorter leg is called a Cathode and this is connected to Ground, often referred to as GND or sometimes as minus sign (-)

For our first circuit we shall use the wires connected to 3V and GND. Insert the LED as per the image, with the long leg to the top of the board, the short leg of the LED should be inserted a few rows down the board.

Hardware_Test

Now take the wire from 3V and connect it *in-line *with the long leg of our LED. Now take the wire from GND and connect it*in-line* with the short leg of our LED.

Your LED should now light up.

Congratulations! You have successfully built the kit.

Flashing an LED

 

Step 1 – Building the circuit

To continue our success, lets build a simple circuit, in fact it’s so simple we only need to alter one wire from our test project.

The red wire that is connected to 3V will need to be removed from the circuit. In its place on the breadboard we shall use the wire from Pin 0 on the micro:bit.

Step 2 – Coding the project

On your computer, open the web browser and visit https://www.microbit.co.uk/create-code

 

microbit-website

Click on the New Project link in the Microsoft Block Editor section to open a new page, this new page is our coding suite but it is rather blank Looking to the left we can see a series of menus.

Project1-1

Look for the menu labelled “Loops”, left click on the menu and a series of blocks will appear. Using the left mouse button, click and drag “while true” from the menu and drop it into the main window. This loop will run any code inside of it forever.

Look back at the menu, find the Pins menu, left click on it and choose “digital write (0,1) 1 to pin P0”. Drag this block and drop it inside of the loop. By default this block is set to turn on Pin 0\. In coding 1 is equal to on and 0 to off.

Now look for the Basic menu, left click on it and choose the “pause (ms) 100” block, drag it and place it under the previous block but inside of the loop. Milliseconds is abbreviated to ms and 100 is equal to a tenth of a second. To change the pause to 1 second, left click on the value and type in the 1000\.

From the Pins menu grab another “digital write (0,1) 1 to pin P0” and place it under the pause block. This time change the value of “1” to “0” indicating that we wish to turn the pin off.

Finally add another “pause (ms) 100” block under the previous block but still inside the loop.

Your code should look like this.

Project1-2

Step 3 – Getting the code on to our micro:bit

To start this step you will need to attach your micro:bit to your computer using a micro USB to USB lead.

When a micro:bit is attached to a computer it appears as a standard USB flash drive, handily enabling us to copy our code on to it, but where is our code?

To download a version of our code we first need to click on Compile, this will trigger the micro:bit website to compile, which means convert our code into something that the micro:bit will understand, our code and then trigger a download to our computer.

To add that code to our micro:bit we shall now copy the downloaded file onto the micro:bit flash drive.

Your micro:bit will flash a few times as the code is uploaded to the board. Let this process run until complete. Once complete you will see that the LED attached to Pin 0 is now flashing rather fast, indicating that we have successfully created the project.

Raspberry Pi Zero Networking Solutions

Having been lucky enough to get a Pi Zero when they were first released on the cover of the MagPi magazine, we’ve had plenty of time to try out a number of different things on it.

During that time, one of the things that we’ve always had to consider is the question of how to get our Pi Zero on the network, so we’ve spent a little time investigating some of the different networking options for the Pi Zero.

Continue reading Raspberry Pi Zero Networking Solutions

BBC Microbit

We’ve been trying to get our hands on a BBC Microbit for quite some time now; especially after seeing a whole wall of them at the recent BETT Show in London a couple of weeks back.

BBC Microbit Wall
BBC Microbit Wall

Anyway, our good friends at Kitronik recently sent us a bundle of BBC Microbit goodies to play with; including their excellent Inventor’s Kit, a couple of different cases, a Prototyping System, breakout boards, and a motor driver board. It was like Christmas all over again.

Continue reading BBC Microbit

PaPiRus ePaper Hat

We’ve been having a bit of a play with the new PaPiRus ePaper HATs from Pi Supply. These are basically small displays (they come in different sizes; 1.44″, 2.0″ and 2.7″) which use ePaper technology, and sit on the Raspberry Pi GPIO header. The great advantage of the ePaper (aka eInk) technology is that you can display information on the screen indefinitely; even after the power is switched off.

Continue reading PaPiRus ePaper Hat