Installing Scratch GPIOThe standard version of scratch can't control the GPIO, so you need to install ScratchGPIO7 before you can play with LED's and switches. I googled the install instructions but here they are if you need them:-
wget http://bit.ly/1wxrqdp -O isgh7.sh
sudo bash isgh7.sh
If you need to add the desktop icons to other users you can specify user name on that last command:-
sudo bash isgh7.sh fred
(nb. make sure you have an internet connection first!)
Use the new desktop icons to start scratch and it will ensure a background python task is running that enables the GPIO control.
There's a simple code example that toggles pin 11 between low and high. We attached an LED to this pin, (using a 470R resistor) and connected the other end to one of the ground pins. (doesn't matter which)
Success - a flashing LED!
Moving on from BasicsThe plan was that I'd set my 8 year old a task of driving one of his 7 segment displays using the Raspberry Pi, and maybe get it counting 0 to 9.
It seemed like a suitable jump of of complexity both in software and breadboard wiring. (perhaps it was a jump too far) Here's what happened!
Circuit DebuggingMy son disappeared for half an hour and returned with breadboard, 7 segment display, and T-Cobbler carefully connected with jumper wires. He plugged it in to the RPi, loaded up the pin 11 demo program and clicked the green flag icon to run the program. The scratch variable was clearly cycling on and off on the screen, but none of the segments lit. We ended up tearing the circuit down trying to get it to work and along the way discovered a few problems..
1. The T-Cobbler board has 40 pins that need to be inserted into the breadboard. That takes more insertion force than you'd think.
2. The LED display was of the common anode variety. This means that all the anodes are connected together and the segments lit by pulling the individual cathodes down to ground.
(So he had connected the LED segments the wrong way round.)
A quick flick through the Maplin catalog confirmed you can get common cathode versions.
Driving Common Anode DisplaysMy initial thought was we'd have to use a transistor inverter to drive each segment, but that's a fair bit of extra complexity and not strictly necessary.
|A simple inverter circuit is not necessary.|
Each GPIO output actively pulls high or low, so if you connect the common anode to 3.3v then any low pins connected to the cathodes will turn the display segment on.
|Connecting a common anode display to a Raspberry Pi|
The only hitch is the logic is the other way around! (pin low = segment on)
But it's only just occurred to me that we could do this after reading the Raspberry Pi GPIO Documentation and seeing an LED connected this way. (Must try this tonight!!)