Scratch GPIO Development

This blog entry is for me to provide feedback on what’s happening in developing my Scratch GPIO hander set-up.

This code WILL PROBABLY NOT be compatible with the 1.x release and is intended for experimenters and helpers.

Its for both coders with suggestions and importantly users (particularly teachers/educators/parents with kids) to provide input into what the handler does and the syntax used to do it.

My development code is available on Git Hub https://github.com/cymplecy/scratch_gpio

I’d welcome collaborators who share my vision to enable 10 year olds and younger to make things flash/beep/turn/step using a Raspberry Pi and some cheap components. This project is NEVER going to be an I2C bus controller ๐Ÿ™‚ย  (But as Sean says – never say never again!)

I’ve created a ScratchGPIO login on the Scratch Site so that we can all start to upload and share Scratch GPIO code – I want to share the password with any educator who’d can contribute examples and lessons – just contact me for it ๐Ÿ™‚

Changes made from 1.x codebase
All pins now default to inputs until addressed as outputs and then they are switched dynamically between digital, PWM or Stepper Motor mode as needed

Any pin can now be used for DC motor or variable brightnes LED using PWM as I now use the a threaded libary PyzPWM

(Although I expect to start using the PWM within RPi.GPIO now that Ben Croston has added it in ๐Ÿ™‚ )

PWM output is controlled by variables starting with “Power” e.g Set power11 to 50ย  will set pin 11 to a 50% duty cycle and effectively give an average 1.65V instead of 3.3Vย  (MotorA/B on pins11/12 retained for simpler syntax for younger pupils)

“Power” is more generic and can be applied even when just varying a LEDS brightness (prompting for this change came from Aaron Shaw’s RGB LED MagPi article http://issuu.com/themagpi/docs/issue10final Page 26)

PinPattern usage has been removed at this time as not compatible with the concept of any pin being input or output – needs thinking about how to re-introduce.

Single Pin Ultrasonics – if you connect an Ultrasonic Module as per this diagram, then we only need one GPIO pin to trigger it and receive the returned pulse

ultrasonic1pin

So now you simply use it (assuming connected to pin 23)ย  use broadcast sonar23 and then just use the sensor item sonar23 to get the distance measured in cm

Stepper motors
Currently got 5 wire unipolar steppers addressed using “StepperX” broadcast to tell Scratch that we’ve connected them . “StepperA” means one connected to Pins 11,12,13 and 15, “StepperB” means one connected to Pins 16,18,22 and 7.ย  They are then controlled using normal MotorA or MotorB variables to control their speed but since they are bi-directional, you can use values of -100 to 100.

Also I have syntax for saying stepping a finite number of steps.ย  You need to broadcast a “StepperA” or “StepperB” to initialise as above but then change (not set) variables PositionA orย  PositionB for the numbers of steps you wish each stepper to turn ( using change and not set overcomes a bug/feature of Scratch itself)

Tasks currently in progress

Begining to document

Tasks to be done next but not yet in progress

Add back in PinPattern in some fashion to allow multiple pinout changes in one command

Make sure AllOn /Alloff can still be used

Add global Invert broadcast that inverts all high/on/1 commands to pin being set to 0V and all low/off/0 to set a pin to 3.3V (Needed if dealing with components that need to be dragged to 0V to turn them on)

Add in code to deal with H bridge DC motorsย  (I’d need to build a bot with one first ๐Ÿ™‚ )

Tasks just a gleam in my eye
Add in Servo control (so I can make robot arms wave around)

Plugin in modules for specific hardware (e.g Raspberry Ladder Board, PiBorg, H-Bridge Motors,BerryBoard) etc

Manchester Jamboree – Intro to Robotics Session

On the 9th March, the first every Jamboree was held in Manchester and myself Jason Barnett (@boeeerb) http://www.boeeerb.co.uk/ and Ben Smith (@ManchesterBudo) ran a 45 minute session showing people how to easily interface a Raspberry Pi to a ยฃ5 breadboard setup with some LEDs and a switch.

CPC Components kindly provided all the hardware for the day and we had 16 RPi setup and managed to cram 32 people into the session.

We showed them (and they all then did) how to make a simple set of Traffic Lights and have them go through the Green-Amber-Red-Red and Amber -Green sequqnce and how the RPi could detect if a switch was open or shut.

All of this was done with a normal RPi running my Scratch GPIO package. https://cymplecy.wordpress.com/2012/08/26/scratch-controlling-the-gpio-on-a-raspberrypi/

We explained how as well as switching things off and on, you can use an RPi to vary the speed of a motor by simply adding in a 37p IC (ULN2003) and Jason showed everyone what you can acheive with a bit more time and effort ๐Ÿ™‚


The link to our presentation is here https://docs.google.com/presentation/d/1JqwOkrtjOu6y-uS2w3M1IPbosq6vlAEgl_M7CJqMcJ4/

Please tweet any of us of leave comments here
@cycmplecy (me) @boeeerb (Jason-Master Roboteer) @ManchesterBudo (Proper Teacher)

My email is simplecy at googlemail dot com

Euxton CE Robot Club

Just got two roboteers for this club (I think my advertising skills need improving ๐Ÿ™‚ ) and for this club, I’ve switched to using the @GeekMoore “Magical Moore” chassis to start with as its uses higher geared motors than the ones supplied with the Magician chassis which makes for easier control as the robot doesn’t shoot off across the classroom.

We are still using a WiFi enabled Raspberry Pi as the robot controller and using a remote control program (VNC) to let us program it from a class Laptop.

I just explained to the Roboteers that the robot had 2 motors and that we needed to create 2 variables called MotorA and MotorB and then just prompted Ben and Dylan to work out what they needed to do to get the robot to move. ย  I try and use this technique as much as possible – give them a hint as to what is needed and then let them run with idea and code it up.

These boys have had 3 half-terms of Scratch lessons over Y5/Y6 and have already used Shrimps (Arduinio clones) via netbooks to control LEDs etc so were pretty savvy in this respect.

By end of 1 hours session, they’d got full remote control of the bot (fwd,left,right,stop,speed up, slow down).

So next week, we’ll be hopefully moving onto using IR Line Sensors to get the bot to zigzag along a line by itself ๐Ÿ™‚

http://euxton.mediacore.tv/media/test for video of bot in action

Week 2

We had another Roboteer join the club.

We added an Ultrasonic Sensor to Pi (name given to the bot) and then programmed it toย  turn left for 1 second if it detected an object less than 20cm in front of it.
This turned out to be an excellent concept/idea – very simple to do but very effective.
http://euxton.mediacore.tv/media/week2-in-robot-club

Although we didn’t do it – could be worth getting roboteers to extending their code so that the Robot turns for random amount of time or randomly left/right

Week 3
We switched over at this point to a Magician Chassis as I’d previously equipped it with line IR sensors (Hopefully I’ll get some fitted to the Magical Moore bot over the Easter hols)

The Roboteers soon understood that a black surface didn’t reflect the IR light but that white did and that they could see these changes in Scratch my monitoring Pins 8 and 10.

Since they were fully familiar of setting a motor variable to 0 to get the robot to turn, they soon worked out what was needed once we’d done some runs (the Magician Chassis doesn’t run very straight – well mine doesn’t anyway!)

I showed them if blocks and they coded it up.

http://euxton.mediacore.tv/media/line-following-scratchraspberry-pi-robot-using-inf

Then they came up with idea of getting a pile of books at the end and using the Ultrasonic sensor to make it stop

http://euxton.mediacore.tv/media/raspberry-pi-robot-following-a-line-and-using-ultr

Week 4The roboteers were very keen to get the bot to follow a line around the classroom so we tried using masking tape on the floor and changed the code so that the bot would follow a white line instead of a black one.

But the sensors couldn’t really tell the difference between the grey carpet and the masking tape so we tried painting the masking tape white and put some pieces of cardboard packing in to lower the sensors- the combination worked!ย  Dillon said what we should have had was the same mechanism he had on his pen where you turned the top and the pen nib went in and out – I call that moment one of the learning successes ๐Ÿ™‚

Due to time paint takes to dry ๐Ÿ™‚ we didn’t have time to run the tape around the class (much to roboteers disappointment) but at least we managed to get it to do this

http://euxton.mediacore.tv/media/round-and-round-the-garden

Next time I do this club with another school, we’ll try out using white insulation tape instead of masking tape and I’ll see if that works better and make the sensor heights adjustable (by roboteers – not just me)ย  if possible

This club has been a great sucess