phillipsoasis
Posts: 44
Joined: Wed Oct 06, 2021 10:12 pm

How To make a LED blink using GPIO and PWM?

Sat Dec 09, 2023 12:30 am

I am trying to get an LED to blink using PWM on a Pi Zero 2 W. I have GPIO3 connected through a 22K resistor to the base of a 2N2222 transistor. The LED and a 510R resistor are connected to the collector, and the emitter is connected to ground.

When I set GPIO3 high, the LEDs turn on. When I set it low, the LED turns off.

When I try this code, the LED turns off and never comes on again

Code: Select all

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.OUT)
p = GPIO.PWM(3, 1)   # 1 Hz, or 1 blink per second
p.start(50)        #   50% duty cycle  
Can I have PWM on any GPIO pin, or do I have to use GPIO18 (board pin 12)? There is a lot of contradictory information out there regarding PWM!

cleverca22
Posts: 8607
Joined: Sat Aug 18, 2012 2:33 pm

Re: How To make a LED blink using GPIO and PWM?

Sat Dec 09, 2023 1:41 am

phillipsoasis wrote:
Sat Dec 09, 2023 12:30 am
Can I have PWM on any GPIO pin, or do I have to use GPIO18 (board pin 12)? There is a lot of contradictory information out there regarding PWM!
https://elinux.org/RPi_BCM2835_GPIOs

for hardware pwm, you can only use it on certain pins

channel 0 can be used on gpio12, 18, or 40
channel 1 can be used on gpio 13, 19, 41, or 45


software pwm can be used on any pin, because its just software turning a gpio on&off really fast
but software pwm uses a lot of cpu, and the duty cycle will vary with cpu load

phillipsoasis
Posts: 44
Joined: Wed Oct 06, 2021 10:12 pm

Re: How To make a LED blink using GPIO and PWM?

Sat Dec 09, 2023 5:38 am

I only want the LED to blink for about a minute and then the Pi will shutdown. What am I doing wrong in my code, because it does not work.

cleverca22
Posts: 8607
Joined: Sat Aug 18, 2012 2:33 pm

Re: How To make a LED blink using GPIO and PWM?

Sat Dec 09, 2023 5:43 am

phillipsoasis wrote:
Sat Dec 09, 2023 5:38 am
I only want the LED to blink for about a minute and then the Pi will shutdown. What am I doing wrong in my code, because it does not work.
i wouldnt bother with pwm when going as slow as 1hz
just use a for loop and sleep

pcmanbob
Posts: 13746
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: How To make a LED blink using GPIO and PWM?

Sat Dec 09, 2023 12:55 pm

The reason your code does not work.

Once your PWM is set you exit the program which results in the PWM stopping, you need to keep the program running to keep the PWM running.

So this code will drive the LED for 10 seconds using gpio 3 tested and working

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.OUT)
p = GPIO.PWM(3, 1)   # 1 Hz, or 1 blink per second
p.start(50)        #   50% duty cycle  
time.sleep(10)
The only difference in my test circuit is I used a 1K resistor between the gpio pin and the transistor base.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

phillipsoasis
Posts: 44
Joined: Wed Oct 06, 2021 10:12 pm

Re: How To make a LED blink using GPIO and PWM?

Sat Dec 09, 2023 10:25 pm

I tried what you suggested and tested, but it didn't work. No worries, as I replaced the PWM with a simple Python daemon thread that flashes the LED wile the Pi shuts down. Works great! Thanks again for your efforts!

cleverca22
Posts: 8607
Joined: Sat Aug 18, 2012 2:33 pm

Re: How To make a LED blink using GPIO and PWM?

Sat Dec 09, 2023 11:25 pm

phillipsoasis wrote:
Sat Dec 09, 2023 10:25 pm
I tried what you suggested and tested, but it didn't work. No worries, as I replaced the PWM with a simple Python daemon thread that flashes the LED wile the Pi shuts down. Works great! Thanks again for your efforts!
also keep in mind, the rpi firmware already does just this!

when the shutdown is complete, it will blink the status led 10 times, then halt

phillipsoasis
Posts: 44
Joined: Wed Oct 06, 2021 10:12 pm

Re: How To make a LED blink using GPIO and PWM?

Sun Dec 10, 2023 6:16 am

I know that. It is just a little hard to see that LED from 30 feet away! I need a bigger LED.

Return to “Python”