Robotics for Beginners 1 – An introduction

Hello everyone and welcome to my series about getting started with robotics and the Raspberry Pi. In this series you will learn how to make a standard two or four wheeled robot, and make it smarter and smarter with sensors! This series is ideal for anyone who has/intends to purchase the CamJam Edukit 3. So, let’s get started!

Okay, when I say the word ‘robot’ I’m sure many of you will reminisce about massive killer machines, Daleks, and maybe even Wall-E. However, I am sorry to say we will not be building any of those. By ‘robot’, I really mean a Raspberry Pi controlling motors that are screwed into a chassis which is usually made out of some form of plastic or wood, however you could get more creative and make a chassis out of anything! In order to build your first robot you will need:CRC8BgUWwAAretf

A Raspberry Pi – The model A+ is ideal due to it’s small size and lower power consumption compared to other models. It also has a 40 pin GPIO header which is great for adding sensors, or customising your robot with blinky LEDs!

Motor Controller Board – This will plug in to the GPIO pins of the Pi. There are many brands and models available and I will try* to write code for the several models available to ensure tutorial compatibility. If you don’t have an MCB, I personally recommend the one from Ryanteck as control is by turning on and off GPIO pins.

Motors – Generally, most Pi robots use yellow DC gearbox motors from China which can be purchased from Ebay for around £6 a pair.

Power – You will need to find a portable way to power your Pi as well as your motors, otherwise the robot will be stuck to a power cable! You can power both your Pi and motors from batteries, however you will need some sort of switching regulator to scale the voltage down to the 5V the Raspberry Pi needs, otherwise you are at risk of damaging it. The other method is using 4 AA batteries (if your motors require 6V, if they require a different voltage just adjust the number of batteries), as well as a battery pack, the things normally advertised as being portable phone chargers.

Controller arrangement – There are many different ways of doing this; your robot could be autonomous meaning that once the code is run, you don’t have to control it, however that is less fun! I will teach you how to use a Wii controller to move your robot, but you could also use a wireless keyboard or an arcade joystick.

Chassis – Like said above, a chassis can be anything!

Sensors (optional) – Sensors can be used to create robots that can avoid obstacles or follow lines! There will be tutorials to control these later on after we have actually built our robot!

Robots can be controlled using python with gpiozero, or even ScratchGPIO, a visual programming language so even the youngest of programmers can become robotics engineers! Through building a robot you will learn programming concepts such as loops, debugging, if/else statements and functions!

Thanks for reading and if you have your own robots, tweet pictures to me @RPi_Yaz14 !

 

GPIOZero Tutorial 2 – Programming Traffic Lights

Hello again, and welcome to another Raspberry Pi tutorial! Today, you are going to learn how to wire up and program a set of traffic lights using gpiozero on the pi!

For this tutorial, you will need a breadboard, 3 LEDs in traffic light colours (red, yellow and green), 3 resistors (most likely 330 ohm) , an  active buzzer (some buzzers require resistors, so be sure to check yours – if you are using buzzers from the CamJam edukit you will be fine!) You will lastly need 4 male to male jumper wires and 5 male to female jumper wires. Make sure that your buzzer is an ‘active’ buzzer, as an ‘active’ buzzer only requires current of any type to go through it in order to make a sound. ‘Passive’ buzzers will require an oscillating current which is not provided from the Pi. Here is a circuit diagram for the traffic lights:

traffic lights

Again, like the last tutorial, you can use different pins to the ones I have used, but you must remember to use generic GPIO pins (and not any of the power, ground etc) for the positive ends of the components. The jumper wire connected to the ground leg of each of the components can be connected to any ground pin. I have chosen GPIO pins which make will make the python code compatible with all models of Raspberry Pi, from the A right up to the Pi2 Model B. For the traffic lights I have chosen these GPIO pins (BCM) :

Red LED: 24

Yellow LED: 10:

Green LED: 9

Buzzer: 3

Now that we know what pins our components are going to be using, let’s crack on with some gpiozero code! Like before, we are going to use a ‘while’ loop to run our program forever, however the ‘while True:’ line we will be using in the code can be replaced by a ‘for’ loop, which specifies how many times the traffic lights will be run. I will show you how to do both!

 

Here is the method that will cause the program to loop forever

from gpiozero import LED, Buzzer
import time

red = LED(24) # telling the pi which GPIO pin we have connected each component to
yellow = LED(10)
green = LED(9)
buzzer = Buzzer(3)



while True:

    red.on
    buzzer.on
    time.sleep(3)
    buzzer.off
    yellow.on
    red.off
    time.sleep(0.5)
    yellow.off
    green.on
    time.sleep(10)
    green.off

Here is the method where the traffic lights will run for a set amount of loops:

from gpiozero import LED, Buzzer
import time

red = LED(24) # telling the pi which GPIO pin we have connected each component to
yellow = LED(10)
green = LED(9)
buzzer = Buzzer(3)



for x in range(10) # this program will run 10 times, change the number accordingly
    red.on
    buzzer.on
    time.sleep(3)
    buzzer.off
    yellow.on
    red.off
    time.sleep(0.5)
    yellow.off
    green.on
    time.sleep(10)
    green.off

Thanks for reading guys, tweet me your working traffic lights @RPi_Yaz14 ! See you next time!

-Yasmin

GPIOZero Tutorial 1 – Controlling an LED

Hello everyone and welcome to PioTex! This little blog will post tutorials and for the anything to do with the Raspberry Pi! Today I will be teaching you to control an LED using GPIOZero; a library composed by Ben Nuttall at the foundation who aims to make using the GPIO on the pi simple, and more accessible to beginners.

Firstly, you will need to wire up an LED and for this you will need an LED, breadboard, 330 (or similar) ohms resistor, 2 male to female jumper cables as well as a pi and peripherals. LEDs can be wired without a breadboard, just with 2 female to female jumper cables, however it really depends on what you have available, so do whatever method suits you best!

Here is a circuit diagram for wiring upLED the LED:

The longer, positive leg of the LED (or anode) should be connected to the resistor and then to a GPIO pin, which can be any, however I have used GPIO10. The shorter, negative leg (or cathode) is simply connected to ground. If you want to remember which pin is which, you can print off a raspberry leaf, get a Portplus keyring etc.

After wiring the LED, double check it to make sure the wiring is correct, as you have a risk of damaging your pi through incorrect wiring. When you are sure it is correct, boot up your pi into either the graphical environment or command line, it really does not matter which as Raspbian Jessie now supports sudo-less GPIO access, meaning that you do not need to be a super user on the pi in order to access the GPIO. If you are in the graphical environment, navigate to programming, then Python3, then press control + n for a new document. If you are in the command line then type “nano led.py” to create a new document.

In this program we will code the LED to turn on, wait one second and turn off, making the LED effectively blink.

To do this, copy this code into your python window, remembering to change the LED pin number to the GPIO pin you have selected (GPIOZero uses BCM and not Board for labelling the pins

from gpiozero import LED
import time
led = LED(10) # change this to whatever pin you have selected

while True: # making the program loop forever
# indent the next 3 lines by pressing tab or space 4 times
led.on
time.sleep(1)
led.off

Thank you for reading! See you next time with some more GPIOZero tutorials!

-Yasmin