We are going to make a robot which beeps and has a flashing antenna using a Raspberry Pi.
Making an antenna for your robot
Let’s get hands-on with electronics! This is where the Raspberry Pi comes in handy. You will program a small light called a LED (Light Emitting Diode) to flash. First you will need to make a circuit. Ensure that your Raspberry Pi is switched on and ready to go.
- The LED has a short leg and a long leg. Slot a jumper wire onto the long leg.
- Slot the resistor into the other end of the same jumper wire.
- Add another jumper wire the other end of the resistor.
- Take another jumper wire and slot one end onto the short leg of the LED. You should end up with something that looks like this:
- Find pin 1 and pin 6 on your Raspberry Pi using the diagram below:
- The general purpose input ouput (GPIO) pins on the Raspberry Pi speak and listen to the outside world and can be controlled or programmed. Each pin has a specific role. To make life easier the pins are numbered for reference. Pin 1 is for power. Pin 6 is for grounding.
- Plug the resistor jumper wire into pin 1 on your Raspberry Pi and the other jumper wire into pin 6.
- Plug in the micro USB power supply and you should see some text appear on your screen.
How the light antenna works
Now you have a circuit and the LED should be on. If it is not, make sure that you have plugged the jumper wires into the correct pins by checking the diagram above.
So, why does the LED shine?
When the circuit is plugged into the Raspberry Pi GPIO pins, electricity can flow through it. This flow is called the current. The LED only lights up when electric current flows from the long leg through the bulb to the short leg.
The resistor reduces the amount of electric current passing through the circuit. This protects the LED from breaking, as a high current would make the light shine more brightly and then stop working.
Well done! You’ve made the antenna for your robot. Now we need to do some programming to control it.
Make the antenna flash
Now you have an antenna that lights up, you can write a program to tell the LED when you want it to be on.
For this section you will need to use pin 11 rather than pin 1 to power your LED. Pin 11 is special as it can switch power on and off – if you tell it to! Follow the instructions below to learn how to switch pins.
- Shut your Raspberry Pi down and remove the power cable. Move your jumper wire that is connected to a resistor from pin 1 to pin 11. See the diagram below to make sure that your circuit is correct:
- Connect the power cable to the Raspberry Pi and, when prompted to log in, type:
Login: pi Password: raspberry
- Once you have logged in, type startx to load the desktop.
- The desktop should have a ScratchGPIO icon. Double click it.
- If prompted click OK when the remote sensor connections are enabled.
- Right-click on the Scratch cat and choose delete from the menu.Then click on the button for a new sprite and choose robot3 from the fantasy folder.
- Click on control in the top left display. Drag the when Sprite1 clicked block onto the scripts area. Then click on Sound and drag the play sound block onto the scripts area and connect it to the control block.
- Click on the Sounds tab above the scripts area and then click on Import. Choose Electronic and then ComputerBeeps2. This will add it to the sounds tab.
- Now go back to the scripts area by clicking on the scripts tab. Click on the drop down box next to play sound. Choose the sound you just imported from the menu.
- Drag a wait 1 second block onto the scripts area and connect it to the broadcast block.
- Test your program by clicking on the robot sprite. You should see the LED shine and stay on.
- Drag another broadcast block onto your scripts area and connect it to the wait 1 second block. Click on the drop down menu on the broadcast block and select new.In the message name box type pin11off This will switch off the LED.
- Now add another wait 1 second block to the script.
- Test your program again by clicking on the robot sprite. You should see the LED turn on for one second and turn off for one second.
For more detail: Robot Antenna