Archive for the ‘Coding’ Category

PostHeaderIcon Raspberry Pi Robot – Flashing LEDs – The Code

Below you will find the example Python code that we will be using to flash the two LEDs that we wired up in the previous post.

 

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(16, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)

GPIO.output (16, True)
time.sleep(1.5)
GPIO.output (16, False)

GPIO.output (18, True)
time.sleep(1.5)
GPIO.output (18, False)

GPIO.cleanup()

 

The first two lines of code are to import the Raspberry Pi module and the Time modules to enable us to use the Raspberry Pi and Time related specific functions.

The next line is to set the GPIO pinouts up.  Since we are using the standard Raspberry Pi pinouts we just set this as BOARD.

After that we will define which of the Raspberry Pi GPIO pins we will be using as output pins.  As you may remember from the previous post we will be using pins 16 and 18.

The next block of code will send power to pin 16, wait for one and a half seconds before turning the power off again.  These few lines of code will therefore light our red LED for one and a half seconds before turning it off.

The final block of code is exactly as above but it will light our green LED for one and a half seconds before turning it off.

Finally we will run GPIO.cleanup() which will turn off the pins we have been using and return them back to the default state for next time we want to use them.

Once you have got your code typed up and saved as LEDs.py from within a terminal prompt run the following command and watch your LEDs light up in turn for one and a half seconds.

 

sudo python LEDs.py

 

That is all for this post if you have any issues with this code please leave a comment below.

PostHeaderIcon Raspberry Pi Robot – Flashing LEDs – The Circuit

In this brief guide we will show you how to connect up two LEDs to the GPIO ports on your Raspberry Pi and control them via Python Script.  From this point onwards I will assume that your Raspberry Pi has a fully working install of Raspbian and is connected to your WiFi or Wired Network.  Rather than working directly on the Pi I will be connecting via SSH to create and execute our code.

You may be wondering why we are bothering tinkering around with LEDs rather than diving straight in and getting some motors up and running.  There are two reasons for this the first is I am still waiting for my L298N motor Controller to arrive.  The second reason is that the basic Python code we will be using to light a few simple LEDs will be pretty much the same code we will be using to turn a motor on and off.

Below you will find a list of items we will need for this project.

Amount Part Type Properties
1 Red LED package 5 mm [THT]; color Red; leg yes
1 Green LED package 5 mm [THT]; color Green; leg yes
2 270Ω Resistor package THT; tolerance ±5%; resistance 270Ω; pin spacing 400 mil; bands 4
1 Raspberry Pi B rev1 processor Broadcom SoC BCM2835 ARMv6; variant Raspberry Pi B(Rev 01)

Below is a diagram of how we will be connecting our components together on our breadboard (protoboard).  Connect the Ground (Black cable) to the Ground connector on the Raspberry Pi GPIO, this is the third pin down on the side nearest your breadboard.  Plug both LEDs and resistors into the breadboard as shown and the connect the live (or signal cables) to GPIO pins 16 (for the Red LED) and 18 (for the Green LED).  Once that is complete connect the live cables into the slot below the longer leg (Positive) of each LED as shown.  If everything looks exactly like it does in the diagram then it is time to move onto the coding part of this guide.

Flashing LEDs_bbLater in the week I will post the code that we will be using to turn these LEDs on and off and explain what the code does and different ways we can improve it.

PostHeaderIcon Raspberry Pi Robot – Project Overview

After being introduced to Raspberry Pi controlled robotics by Chris over at Bytemoose.co.uk I have (after owning a Pi since launch) finally decided to do something useful with my Raspberry Pi.

The basic goal of this project will be to create a remote controlled vehicle that can be controlled with a smartphone over wifi using a web server that will be running on the Raspberry Pi.

Once this basic goal has been met the project could (and probably will) be expanded to install a webcam on the robot or the addition of an ultrasonic sensor to stop unwanted collisions.

This should prove to be an interesting and informative project as I have very little electronics experience other than what I can vaguely remember from secondary school well over fifteen years ago.

Components:

  • Raspberry Pi – The “brains” of the project this will drive the controller that runs the wheels.
  • USB wifi adaptor – To enable the robot to roam free rather than be tethered via a cable.
  • USB Battery Pack – To power the robot and free the robot from being tethered via a cable.
  • AA Battery Pack – To power the robot and free the robot from being tethered via a cable.
  • AA Batteries – To power the motors and motor controller.
  • Breadboard – This will be used for prototyping and testing components.
  • Zumo Chassis Kit – A two tracked platform that we will build our robot on.
  • 2x 50:1 Motors – The motors that drive the tank tracks. One per side.
  • L298N Controller – This will control the speed & direction of our motors.

In the near future I will be making further posts and guides on how I have managed to bring my robot to life.

Stay tuned for further articles that will hopefully include subjects such as:

  • Setting up your Pi from scratch. (including wifi and remote access).
  • Building the Zumo Chassis for our robot.
  • Connecting the L298N to our Raspberry Pi and Motors.
  • Example Python code for movement.
  • Installing the webserver on the Raspberry Pi.
  • Creating the scripts for movement.
  • Bringing the web server and scripts together.

Raspi