PiMagic – Using it

Getting started with the PiMagic
– PWM Control of an RGB LED with Python, Firmata and the PiMagic

First of all, install Raspbian (recommended) and expand the file system (plenty of references on this elsewhere)

sudo apt-get upgrade
sudo apt-get update

… will ensure that the software you have installed is up to date before beginning with the interesting stuff.

Now we need to ensure that the Raspberry Pi itself isn’t trying to use a console over the UART port /dev/ttyAMA0  There are a number of steps to do this:

  1. Modify /etc/inittab to comment out the following (by adding the #);
    #T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
  2. Modify /boot/cmdline.txt to edit out the console=ttyAMA0, 115200 reference – is should now look something like;
    dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
  3. Modify/add the following line to /etc/sysctl.conf;

Modify these files using nano e.g. sudo nano /etc/inittab  make the changes then CTRL+X and Y to save the buffer.
You’ll need to use sudo since these files can’t be written with standard privileges.

Now reboot (sudo reboot) and check the system boots back up okay.

By default the ATMEGA328P chip should have the Arduino bootloader and standard Firmata software flashed (more on this later if it’s not).  Having Firmata installed means you can control the inputs and outputs via Python pretty easily.

To install the Firmata Python libraries (and relevant dependencies):

sudo apt-get install python-serial mercurial
sudo apt-get install python-pip
sudo pip install pyfirmata

Now, download the test python app for controlling an RGB LED as follows:

curl -O https://raw.githubusercontent.com/croztech

Now shutdown your Raspberry Pi with sudo shutdown -h now

Disconnect the power and plug the PiMagic onto the top of the Raspberry Pi.  Be careful to ensure that the header is not offset.  Also be careful to ensure the power is disconnected when plugging or unplugging the PiMagic.

Connect an RGB LED with anodes to pins D3,5,6 via 220R resistors and the cathode to 0V.  A breadboard setup should look something like this (you can’t see where the black wire goes in this picture but it’s back to 0V on the PiMagic)

Now power back up your Raspberry Pi and log in as normal.

On the PiMagic, the onboard green LED indicates power.  The orange LED is connected to D13 and may blink a few times on initial boot (showing Firmata is active).

To run the code:

python firmata_rgb.py

… and you should see pretty colours cycling around randomly !

Getting started with the PiMagic
– PiPong with analogue paddles

Pong is not only a classic game, but a frequently used tutorial for getting started in game authoring using Python and the Raspberry Pi.  However, I thought it was lacking something… control of the bats using old-school analogue paddles rather than the keyboard.  In this section we’re going to use the analogue inputs of the PiMagic to read analogue voltages from two potentiometers (the paddles) and use this to control the bats.

First of all, Pong is a graphical game, so fire up the GUI from the command prompt with startx

I think Geany is a much better development environment than IDLE so start a LX Terminal prompt and enter sudo apt-get install geany to install this environment.  When done Geany will appear under the Programming menu with the other tools.

Now wire up some potentiometers so that the wiper is connected to an analogue input (e.g. A0), and the extreme ends are connected to 5V and 0V on the PiMagic.  That way as you rotate the pot to move the bat, the output varies linearly from 0V to 5V.  The potentiometers should be single-gang, linear types with a resistance value in the range of 1k to 50k (I used 10k).

Here’s a few pictures of the potentiometers cabled up and mounted in small hand-held plastic boxes.  Note that I cut and used some link wires to make convenient pins for plugging into the PiMagic headers – the 0V (screen of the cable) and 5V (red wire) are paralleled up to make easy the connections.

PiPongPaddles01 PiPongPaddles02 PiPongPaddles03 PiPongPaddles04

Once the hardware is up and running, you can download the source with the following command…

curl -O https://raw.githubusercontent.com/croztech

…and then see what’s going on and launch within Geany.   Happy old-school gaming…

Analogue PiPong



One Response to PiMagic – Using it

  1. […] PiMagic – Using it […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: