childs0
Posts: 5
Joined: Wed Jul 18, 2012 12:14 pm

Python Script to Test GPIO pins

Tue Nov 25, 2014 8:34 pm

Hi,

Ever wondered if one of your GPIO pins is not working, the python script below (and a couple of basic components!) should help.

You will need:

1 LED
1 500 ish ohm resistor
1 breakout board (to avoid having to jab wires into the pi)
Couple of jumper leads

Usual GPIO library.

Connect the resistor to ground, one end of the LED to the other end of the resistor, attach the jumper lead to the other end of the LED, to test that the LED is the right way round, attach the jumper lead to +3.3v to see if it illuminates, if not switch the LED the other way around.

Run the script and place the jumper cable over each of the gpio pins in sequence, you should see the LED blink. If it does not blink (stays off or on) then chances are is that that GPIO pin is fried, as happened to me! (for reference, putting +5V through it will do that!)

Please note that I think some pins (I think) are fixed as non GPIO use and may show half illumination if they are digital lines or other behaviour.

The script below will setup the GPIO pins in output mode then cycle through switching them all on and off in a half second loop and print the gpio states to screen.

The start and end GPIO pins can be set via GPIO_start/end in the script.

This is my first python code from scratch so sorry if it 'doesn't read nice'

Code: Select all

import RPi.GPIO as GPIO
import time

gpio_start = 1
gpio_end = 27

GPIO.setmode(GPIO.BCM)

for x in range (gpio_start, gpio_end):
	GPIO.setup(x, GPIO.OUT)

while True:
	
	for y in range (gpio_start, gpio_end):
		GPIO.output(y, True)
		print 'on ', y
	time.sleep(.5)
	
	for z in range (gpio_start, gpio_end):
		GPIO.output(z, False)
		print 'off ',z
	time.sleep(.5)
Let me know if this is useful!
Last edited by childs0 on Tue Nov 25, 2014 10:08 pm, edited 1 time in total.

User avatar
davidcoton
Posts: 5026
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: Python Script to Test GPIO pins

Tue Nov 25, 2014 9:09 pm

the LED to ground, attach the jumper lead to the end of the LED, to test that the LED is the right way round, attach the jumper lead to +3.3v to see if it illuminates, if not switch the LED the other way around.
By that stage you will have a fried LED. It should say:
"Connect the resistor to ground, one end of the LED to the other end of the resistor, attach the jumper lead to the other end of the LED, to test that the LED is the right way round, attach the jumper lead to +3.3v to see if it illuminates, if not switch the LED the other way around."
Signature retired

childs0
Posts: 5
Joined: Wed Jul 18, 2012 12:14 pm

Re: Python Script to Test GPIO pins

Tue Nov 25, 2014 10:08 pm

good spot, it was a test....

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Python Script to Test GPIO pins

Wed Nov 26, 2014 1:15 pm

I created a GUI that allows you to test GPIO pins a while back.

You can use it to test both inputs and outputs.

http://www.raspberrypi.org/forums/viewt ... er#p377649
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Advanced users”