I have a setup like below:

enter image description here

The script is supposed to turn the led on, sleep and turn it off again. And the script does work.

import RPi.GPIO as GPIO
import time

# This script will turn on the LED for 1 second and turn it off again

# setup GPIO
# disable warnings
# set GPIO.OUT to pin 18

# turn on LED
print "LED on"
# wait for 1 second
# turn off led
print "LED off"

If I reboot the pi the led suddenly is on again and I wonder why. Is it because I am missing something important about the pi's functionality or am I making mistakes here? By the way the same behavior occurs with rgb leds too.

I also read up on GPIO.setmode(GPIO.BCM) and it seems that the only difference between the modes BCM and BOARD are the number one uses when referring to pins, or might the mode be the issue?

  • I am a complete noob with the RPi hardware, but it just so happens that I might have read up on this very issue last night. As I understand it, the initial state of the GPIO pins on boot can fluctuate. A lot of people dealing with motor controls were particularly concerned about this. There is discussion about this on the RPi forums and jojopi's 12/31/16 reply might be helpful. I researched it far enough to discover device tree overlays but no further.
    – bobstro
    Commented Mar 21, 2017 at 20:34
  • What happens if you set the pin back to INPUT at the very end of the script? Does that fix the issue?
    – stevieb
    Commented Mar 22, 2017 at 13:52

2 Answers 2


You can't guarantee the state of the GPIO pins during the boot process; all sorts of things from the kernel configuration, device tree, modules etc can mess with this.

See Why are some GPIO pins HIGH when the Raspberry Pi boots up?

You could run a script from /etc/rc.local to reset this pin on boot, as described here.


Try to cleanup the GPIO pins to restore them to low state. Try the following code for example:

import RPi.GPIO as GPIO  
import time

PIN = 12


# blinking function  
def blink(pin):

# to use Raspberry Pi board GPIO Board Numbering

# set up GPIO output channel  

# blink GPIO18 10 times  
for i in range(0, 10):  

I have modified the code from the site a bit to match your code's pin numbers.

  • Is there any particular reason why you are using BOARD mode here? Commented Mar 23, 2017 at 19:47
  • @チーズパン I really prefer using BOARD mode for numbering to be generic on all versions of Raspberry Pi
    – Tes3awy
    Commented Mar 23, 2017 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.