ScratchGPIO – Add on Modules/Boards
Scratch Controlling the GPIO Pins on a Raspberry Pi
Part 4 of 4
Ultrasonic Sensor Boards Single Pin Ultrasonics – if you connect a cheap 4 pin Ultrasonic Module as per this diagram, then you only need one spare GPIO pin to trigger it and receive the returned pulse
So now you simply use it (assuming connected to pin 23)
Use followed by a wait 1 and then just use the sensor item sonar23 to get the distance measured in cm. Any pin can be used for this purpose and if you had several of them you could broadcast each of them in turn.
Alternatively, you can use this syntax which will tell your Raspberry Pi to continuously do a distance check every 1 second and update sensor item Ultra8 with the results without any further need to do another broadcast
Gordon’s Ladder Board
This cheap and useful educational board (link) is an ideal introduction to getting kids to turns things on and off and saves having to wire up a breadboard to get going. It does need someone to solder it together and if you don’t have that skill or tools – jsut give a shout out at a RaspberryJam and I’m sure someone will help you out 🙂
If you’ve got one of these then just create a variable called AddOn and set it to Ladder as the first line in your Green Flag event. This tells the system that your using one and then you can use broadcasts such as:
This script will simply turn all the LEDs out and then just switch on every other one.
To vary the brightness of a LED, you can use a Power variable like this example:
The state of the switches can be accessed through the sensing blocks. Scroll down to the bottom and click on the arrow next to slider and you should see switch1 – 4 (they will only appear after you have run a Green Flag event as above)
This board makes it very easy to control Motors and Servos without any extra hardware as well as giving you the ability to switch your Raspberry Pi on and off and power it from AA batteries.
See http://cymplecy.wordpress.com/2013/08/12/scratch-gpio-piglow-support/ for full support info
You can plug in a HMC5883L based I2C device and access the magnetic heading value it outputs. Simply set AddOn to Compass and then use the heading sensor value which is updated 4 times every second
This really cheap and useful board is an alternative to @drogon LadderBoard.
It has 6 LEDs, a switch and a buzzer
To use it just set the AddOn variable to Berry and then you have easy methods of accessing it
[Led1..6] [On/Off/High/Low] e.g broadcast Led1On will make first red led light up
[All] [On/Off/High/Low] e.g broadcast AllOn will make all LEDs and the buzzer come on.
[Buzzer] ]On/Off/High/Low] e.g broadcast BuzzerOn
You can use variables instead e.g Set Led1 On will turn on 1st LED
And also use Power1..6 (values 0-100) variables to vary the brightness as
There is only one sensor value returned and that is called switch (1 if not pressed – 0 if pressed)
This board is a complete robotics controller for your Raspberry Pi based mobile robot.
Whenever I’ve used an SR-04 before, I found that you need one pin to trigger and another pin to receive. Have I missed something somewhere?
Here is where I got it from
Could you please tell me the values of the resistors used to wire up the ultrasonic sensor?
So all the resistors are 10k?
Sorry for my lack of understanding.
the triple reds are 2.2K and the Brown Black Red is 1K.
This is so you get a 5V * 2.2/3.2 == 3.4 V
I’m getting a bit stuck with the Ultrasonic module. I’ve got it connected up as per the diagram, and have added the broadcast in scratch, however I’m unable to find the sensor item Sonar23. I’m assuming it should appear as a sensing block, but I can’t seem to find it. Any ideas where I’m going wrong?
PS, your work is fantastic!!
Click on 2nd last item in the sensing blocks (labeled slider) and then it should be the last item in the list – if its not there – can you see the other pin sesning inputs (10,19,21 etc) there or not
Hmmm, It’s not the last item on the list, but I can see the other pin sensing inputs (23 not there though)
Are you saying broadcast sonar23
Yes, well broadcast Sonar23
I’m a bit befuddled with this one. You don’t fancy switching off and on amdc trying out without the module connected please
I’ve actually left it at work, but will have a try at some point tomorrow, and let you know how it goes.
Thanks for the support thusfar
I cannot get the variables to work with the Berryclip as described, broadcast is fine, any suggests of what I am doing wrong ?
‘You can use variables instead e.g Set Led1 On will turn on 1st LED
And also use Power1..6 (values 0-100) variables to vary the brightness as’
Scratch doesn’t send variable values out if the don’t change.
So I recommend initialising them like
On Green Flag
set led1 to 0
set led2 to 0
And then carry on with rest of your code
Get thanks this works
I accidently left a post on the useful links instead of here! Anyway I was just wondering if I cab use a DS18B20 temperature sensor with Scratch GPIO.
Thx for your work !
Can you make a complete explemple for the Ultrasonic Sensor Boards ?
broadcast Ultra8 (where 8 is pin number used)
say ultra8 sensor value for 2 secs
Wire up like this http://cymplecy.wordpress.com/ultrasonic-sensor/
broadcast Sonic8 (where 8 is pin number used)
wait 1 sec
say sonic8 sensor value for 2 secs
Thx for answer !
I have used the pin number 8 (GPIO14)
I have made :
wait 1 sec
say (“——^” sensor value) for 2 secs <- but i can't select sonic8 in the scroll menu :s
I have Install ScratchGPIO5 software with :
sudo wget http://goo.gl/Pthh62 -O isgh5.sh
sudo bash isgh5.sh
I have run it on the desktop and now :
I can select the sensor in the scroll menu but when i say it i always respond “299”
Do you have an idee ?
have you wired it up like
what pin are you using? you can’t use 3 or 5 as they have pull-ups on them
Thank for your help, I have resolve my problem 🙂
it was the sensor who was defective
I have changed it and now it work 🙂
To make it work i have do that :
say ultra8 sensor value for 2 secs
I have connect the sensor to the pin 8 (GPIO14)
I bought my son an IWM Raspberry Pi Avoidance Robot Smart Car. This works with Python. The ultrasonic sensor is connected to GPIO17 – pin11.
We have tried to get this to work with Scratch, as my son prefers it to python, but with no success. We have downloaded the latest version of Scratch GPIO and we have been able to turn LEDs on and off (broadcast pin11on) and detect external buttons (set button to pin 26 sensor value). But we cannot get the ultrasonic sensor to work (have not moved on to the motor yet – easy part first!)
From your article it is not clear what we should do to use an ultrasonic sensor on pin11. We tried moving the connection to GPIO 23 – pin 16 so that it matches with your code example (broadcast Sonar23). No effect.
Could you please supply some sample code showing what should work?
Hi. Is the Ultrasonic wired up the Pi in this manner http://cymplecy.wordpress.com/scratchgpio/addon-boards/ultrasonic-sensor/
This means only one pin is needed as the trigger pulse is sent out and then the pin is quickly changed to an input to listen for the echo.
At the moment, I don’t support using 2 pins but I could change that I think without too much trouble. Let me know how yorus is wired (or send me a copy of the assembly instructions/circuit diagrams)
Motors controllers suffer from the problem of there being no standard for what is connected to what pin on the Pi 🙁
But if you could let me have that information as well then I can see what I can do.
if it comes with some example Python code- let me have a copy of that as well.
If using this http://www.ebay.co.uk/itm/Raspberry-Pi-Robotic-Car-3-x-PCB-Kit-Control-2-Motor-2-Servo-1-Ultrasonic-UK-/221419163204?pt=UK_Computing_Other_Computing_Networking&hash=item338d9c4e44 then Ultrasonic is set for single pin use on GPIO 17 which is physical pin11 so use
and then check sensor value Ultra11