LED reference chart.

Raspberry-Pi-GPIO-Layout-Revision-2

Below is a quick reference to where the LED`s and photocell need to be plugged into . If you need any help in wiring up LED`s to the Pi the please have a look at Gordons Projects. A quick search on ebay and should find all the components you`ll need. I used a breadboard with a breakout connector so the Pi can be quickly plugged / unplugged . A little note for the wiring the long leg goes to the Pi pin the other to ground. The LED`s I choose are low power so adding a resistor to them left them very dim.

Led name Colour Referance name GPIO pin Gpio name
Reverse 1 White BLED1 8 GPIO14
Reverse 2 White BLED2 10 GPIO15
Indicator Right 1 Yellow SRLED1 3 GPIO2
Indicator Right 2 Yellow SRLED2 5 GPIO3
Indicator Left 1 Yellow SLLED1 19 GPIO10
Indicator Left 2 Yellow SLLED2 21 GPIO9
Break 1 Red Brake1 16 GPIO23
Break 2 Red Brake2 18 GPIO24
Head Light 1 White HLED1 22 GPIO25
HeadlLight 2 White HLED2 24 GPIO8
Photcell Sensor 26 GPIO7
  

Moving the car and having some fun with the lights.

2013-10-05 14.50.35Moving the car and having fun with the lights. 

The first part of the code is just setting up the GPIO and referencing the pins to make life a bit easier. After that we created a couple of function . Function allowed us to add features as we went along.

Function names and what they do. 

all_off  – This function sets all pins except the brake lights. This gives the effect of the parking lights being on when the car is at a stop.

cleanup – Just a standard GPIO cleanup.

BlinkRight / BlinkLeft- Uses a for loop to flash the yellow LED giving the impression of car indicators. It takes 2 variables which are set by the function fleft and fright . In my version it is set to blink 6 times . 1 blink every 0.5 seconds.

forward – Moves the car forward for 1 seconds . The time can be change by setting the value in the sleep function on line 88 . So sleep(5.0) would be forward for 5 seconds. This functions also turns the parking lights off.

backwards – It is the same as forwards but we go backwards. The time can be changed by changing line 98 . This function also turns off the parking lights and turns on the reverse lights too.

fright / fleft – This function moves the car forward and turns the steering in which ever direction you have chosen. It is one the most complex functions for this project. In it we have the car going forward , turn left/right , turn the parking led`s off and calling the BlinkRight / BlinkLeft function aswell.

allLights – this function was designed as a bit of a diagnoses tool but I kept it as it was fun.It flashes all the LED`s on and off 20 times.

The running code.

From line 147 we have the code the allows us to use the functions. First we do create a couple of variables.

Intro – Is just a quick message welcoming to the program and setting out the keys you can use and what they do.

running – This is a Boolean . It is set to True and allows the while loop on line 165 to run.

Now the fun bit 20 lines of code and endless amounts of fun.

First we Print the Intro variable and our while loop starts up because running == True . It sets the parking brakes to on and the car is ready. key is a raw input that asks the user which direction they want to go. I have used an if statement however a switch would work just as well and would have the benefit of being expand more easily than an if statement.

The options we have are :-

  • f    –    calls the forward function.
  • b   –    calls the backwards function.
  • fr  –    calls the fright function.
  • fl   –    calls the fleft function.
  • l    –    calls the allLights function . The time of flashes and frequency can be set here also. We have set to 20 times and the frequency every 0.5 second.
  • q    –   changes the Boolean value to False and kills the while loop.

After that a nice message is printed and GPIO.cleanup does it`s job.

#!/usr/bin/env python
# Pi car python code by julian and kyle milligan
# version 1.0 beta
""" Must be run as root - sudo python car.py """

import time, datetime, RPi.GPIO as GPIO , os

#referance name and gpio pin number
FORWARD = 11
BACK = 12
LEFT = 13
RIGHT = 15
BLED1 = 8
BLED2 = 10
SRLED1 = 3
SRLED2 = 5
SLLED1 = 19
SLLED2 = 21
BRAKE1 = 16
BRAKE2 = 18
HLED1  = 22
HLED2  = 24

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
#Remember to setup the GPIO pin as input or output
GPIO.setup(FORWARD, GPIO.OUT)
GPIO.setup(BACK, GPIO.OUT)
GPIO.setup(LEFT, GPIO.OUT)
GPIO.setup(RIGHT, GPIO.OUT)
GPIO.setup(BLED1, GPIO.OUT)
GPIO.setup(BLED2, GPIO.OUT)
GPIO.setup(SRLED1, GPIO.OUT)
GPIO.setup(SRLED2, GPIO.OUT)
GPIO.setup(SLLED1, GPIO.OUT)
GPIO.setup(SLLED2, GPIO.OUT)
GPIO.setup(BRAKE1, GPIO.OUT)
GPIO.setup(BRAKE2, GPIO.OUT)
GPIO.setup(HLED1, GPIO.OUT)
GPIO.setup(HLED2, GPIO.OUT)

GPIO.setwarnings(False)
#functions

def all_off(): #turns all of the pins off - always call at the end to save battery
    GPIO.output(FORWARD,0)
    GPIO.output(BACK,0)
    GPIO.output(LEFT,0)
    GPIO.output(RIGHT,0)
    GPIO.output (BLED1,0)
    GPIO.output (BLED2,0)
    GPIO.output(SRLED1,0)
    GPIO.output(SRLED2,0)
    GPIO.output(BRAKE1,1)
    GPIO.output(BRAKE2,1)

def cleanup(): #cleans up the GPIO pins
    GPIO.cleanup()

def sleep(sleepTime=1):
    time.sleep(sleepTime)

def BlinkRight(numTimes,speed):
    for i in range(0,numTimes):## Run loop numTimes
        GPIO.output(SRLED1,1)
        GPIO.output(SRLED2,1)
        time.sleep(speed)
        GPIO.output(SRLED1,0)
        GPIO.output(SRLED2,0)
        time.sleep(speed)
    all_off()

def BlinkLeft(numTimes,speed):
    for i in range(0,numTimes):## Run loop numTimes
        GPIO.output(SLLED1,1)
        GPIO.output(SLLED2,1)
        time.sleep(speed)
        GPIO.output(SLLED1,0)
        GPIO.output(SLLED2,0)
        time.sleep(speed)
    all_off()

def forward():
    print("Going forward")
    GPIO.output(BRAKE1,0)
    GPIO.output(BRAKE2,0)
    GPIO.output(FORWARD,1)
    sleep(1.0)
    all_off()

def backwards():
    print ("Going backwards")
    GPIO.output(BRAKE1,0)
    GPIO.output(BRAKE2,0)
    GPIO.output (BACK,1)
    GPIO.output (BLED1,1)
    GPIO.output (BLED2,1)
    sleep(1.0)
    all_off()

def fright():
    print ("Going forward and turning right")
    GPIO.output(BRAKE1,0)
    GPIO.output(BRAKE2,0)
    GPIO.output(FORWARD,1)
    GPIO.output(RIGHT,1)
    BlinkRight(6,0.5)
    sleep(2.0)
    all_off()

def fleft():
    print ("Going forward and turning left")
    GPIO.output(BRAKE1,0)
    GPIO.output(BRAKE2,0)
    GPIO.output(FORWARD,1)
    GPIO.output(LEFT,1)
    BlinkLeft(6,0.5)
    sleep(2.0)
    all_off()

def allLights (numTimes,speed):
    for i in range(0,numTimes):## Run loop numTimes
        GPIO.output(SLLED1,1)
        GPIO.output(SLLED2,1)
        GPIO.output(SRLED1,1)
        GPIO.output(SRLED2,1)
        GPIO.output(BLED1,1)
        GPIO.output(BLED2,1)
        GPIO.output(BRAKE1,0)
        GPIO.output(BRAKE2,0)
        GPIO.output(HLED1,0)
        GPIO.output(HLED2,0)
        time.sleep(speed)
        GPIO.output(SLLED1,0)
        GPIO.output(SLLED2,0)
        GPIO.output(SRLED1,0)
        GPIO.output(SRLED2,0)
        GPIO.output(BLED1,0)
        GPIO.output(BLED2,0)
        GPIO.output(BRAKE1,1)
        GPIO.output(BRAKE2,1)
        GPIO.output(HLED1,1)
        GPIO.output(HLED2,1)
        time.sleep(speed)
    all_off()

#varibles

Intro = "Welcome to Julian and Kyle`s Pi Car program\n\
Please use the following commands to move the car\n\
f = forwards\n\
b = backwards\n\
fr = forwards and turn right\n\
fl = forwards and turn left\n\
br = backwards and turn right\n\
bl = backwards and turn left\n\
l = all ligths on\n\
q = quit\n\
\n\
enjoy."

running = (True)
print (Intro)

while (running == True):

    GPIO.output(BRAKE1,1)
    GPIO.output(BRAKE2,1)
    key = raw_input("which way - ")

    if (key == "f"):
         forward()
    elif (key =="b"):
         backwards()
    elif (key =="fr"):
         fright()
    elif (key =="fl"):
         fleft()
    elif (key =="l"):
         allLights(20,0.5)
    elif (key =="q"):
         running = (False)

print (" Hope you had fun ")
GPIO.cleanup()

  

Light activated headlights for the Pi-Car.

Code for the head lights.

photocell

#!/usr/bin/env python

# Example for RC timing reading for Raspberry Pi
# Must be used with GPIO 0.3.1a or later - earlier verions
# are not fast enough!

import RPi.GPIO as GPIO, time, os

#referance name and gpio pin number
HLED1  = 22
HLED2  = 24
sensor = 26

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
#Remember to setup the GPIO pin as input or output
GPIO.setup(HLED1, GPIO.OUT)
GPIO.setup(HLED2, GPIO.OUT)

DEBUG = 1
GPIO.setmode(GPIO.BOARD)

runningLights = (True)

def RCtime (RCpin):
        reading = 0
        GPIO.setup(RCpin, GPIO.OUT)
        GPIO.output(RCpin, GPIO.LOW)
        time.sleep(1.0)

        GPIO.setup(RCpin, GPIO.IN)
        # This takes about 1 millisecond per loop cycle
        while (GPIO.input(RCpin) == GPIO.LOW):
                reading += 1

        return reading

print ("Light program running!!!")
while (runningLights == True):
        lightSensor = RCtime(sensor)
        #print lightSensor

        if ( lightSensor > 400):
                GPIO.output(HLED1,1)
                GPIO.output(HLED2,1)
        elif ( lightSensor < 400):
                GPIO.output(HLED1,0)
                GPIO.output(HLED2,0)

GPIO.cleanup()

This code was adapted from the tutorial from Adafruit . The only change I have made is setting up the 2 LED`s (HLED1 and HLED2 ) and added an IF statement . The IF statement switch`s on the led`s if the light drops below 400 and of if it`s over 400. I got this figure by hours of experimentation by switching the bedroom light on and off. The print statement is there just so I know the program is running correctly. Hopefully in the future I will ingrate this code with the main code.