Showing posts with label Led. Show all posts
Showing posts with label Led. Show all posts

Friday, November 21, 2025

Flexible led filament a first look

For an index to all my stories click this text.

In a previous story I showed how to work with led filament. These are rigid tubes that contain leds in one color and can be used for lots of projects. You can read that story here: https://lucstechblog.blogspot.com/2025/11/led-filament-first-look.html

There is however also a flexible version available. These usually consist of micro LED diodes mounted on a flexible metal base, coated with silicone for protection. They are constructed in a way that it looks as if the light of the leds shine 360 degrees. The back side of the strip though looks less bright as the front.

The led filament is indeed flexible. Bending the strip to sharp will break it. So no 90 degree corners, just a smooth bend.

These flexible leds are available in several colors: warm white, pink, green, blue and red.
and they can be bought in several lengths. I have seen lengths up to 450mm (45 cm).

Please note each of these led filaments have 1 color. They are not like ledstrips or neopixels. They have a fixed color. So make sure you order the ones you need.

Beware of the contacts.


There are versions available where both VCC and GND contacts are at one end of the ledstrip. And there are versions that have contacts at both ends, just like the rigid versions. So check which version you need before placing an order. Just be careful when experimenting as the contacts are fragile.

They are available from our usual Chinese friends. Adafruit sells them also (although a lot more expensive) and calls then nOOds (from noodles)

Power requirements.

There is surprisingly few documentation available.

There are two versions available. The first works at 12V and the second at 3V. For our microcontroller projects I advise to use the 3V version.

Adafruit suggest you can supply up to 100mA. Chinese suppliers advise a maximum of 50mA.
Better safe as sorry so I suggest stay below the 50mA.

Breadboard setup

I used the same breadboard setup as in the story about the rigid led filament.


Like in the previous story the led filament is powered by a 5V USB power supply. The Raspberry Pi Pico can not supply enough current from it's IO pins so a 2N2222 transistor is used.

The 2N2222 can supply a maximum of 800ma but that is in optimal circumstances with proper cooling. Stay on the safe side and attach up to 4 led filaments to one transistor so the maximum current the transistor needs to supply will be around 200ma.

You can exchange the Pico by an ESP8266 or ESP32 but then you will need to alter the programs to use the right GPIO.

In this setup I used a resistor of 100 Ohm. The USB wall connector supplies 5V. So the resistor reduces the current to 20Ma
If you are using a different power supply or want more (or less) current please revert to the previous story for an explanation of the calculations. You can find it here : https://lucstechblog.blogspot.com/2025/11/led-filament-first-look.html

If you want the leds to be brighter use a resistor of 47 Ohm. That will give around 40mA although I must say I did not notice any difference with my eyes. So I advise to stick at 100 Ohm.

The programs are also the same:

# -------------------------------------------
# simple test for setting filament on and off
# -------------------------------------------

from machine import Pin
import time

filament = Pin(16, Pin.OUT)

while True:
    filament.value(1)
    time.sleep(1)
    filament.value(0)
    time.sleep(1)


This sets the led filament on and off.

# -------------------------------------
# test program for dimming led filament
# with PWM
# -------------------------------------

import machine
import time

filament = machine.PWM(machine.Pin(16))
filament.freq(1000)

while True:
    min = 0
    max = 65536
    for i in range(min,max):
        filament.duty_u16(i)
        time.sleep(.0001)
    #time.sleep(.5)
    for i in range(max,min,-1):
        filament.duty_u16(i)
        time.sleep(.0001)
    #time.sleep(.2)

This program brings the led slowly to full brightness and then slowly dims it and keeps repeating this sequence.

The led filament consists of leds so if your program does not work try changing the VCC and GND contacts at the filament.

My tests.

I bought a 30 cm long pink strip which had both contacts at 1 side.


First I made a circle around a round object.


And here I draped the led filament around a wooden stick with a diameter of about an inch (2.5 cm).

The led filament is fun to play with. The filament is bright and invites to be used in several creative projects.

Till next time
have fun


Luc Volders

Friday, November 14, 2025

Led filament, a first look

For an index to all my stories click this text

This story is about the rigid led filament. There is also a flexible version which will be discussed in an upcoming story.

I saw on the internet something that looks like a small led strip. They have several names: Led Filament, Cob Led Filament and Edison bulb led parts.


They are meant as ledstrips for use inside filament-lamps like the one above. The yellow strips in the lamp is what I am talking about. They can be bought as tiny led strips.

These are actually really small led strips inside a tubing and have contacts at both sides.


The photo just shows the size of this filament compared to a normal led.
This clearly shows they are slim and long.


The filaments are available in several lengths. I have seen lengths up to 68mm (about 7cm)
For testing purposes I bought a batch of 3.8 cm. Well The advertisement said they were 3.8cm long. And that is correct. However that is inclusive the contacts.


The led filament itself is about 3cm.


And they can be bought in several colors. At the moment of this writing the following colors are available: white, warm white, red, pink, purple, green and blue. They are not like neo-pixels or ordinary led strips. Each strip has only 1 color.

If you are going to buy them just make sure you buy the right ones for your purpose as they are available in 3V and 12V and even higher voltage versions. For our purposes we will be working with the 3V versions. As that is the voltage most of our microcontrollers work with.

Fragile

The tubing that encapsulates the leds feels like plastic. It is not flexible but rigid. The tubing can handle some stress but be aware that it is fragile.


As i wanted to know if it was flexible I bend one of the filaments (a bit) and it broke immediately. So be careful when working with these.
Despite being broken it still worked, what amazed me. Only the leds after where the break was, were working as the picture shows.

VCC and GND

Like stated these are led strips. So they need power. And just like normal leds the power needs to be supplied at the right contacts.


Carefully look at the picture.
At the right side there is a small hole in the contact. That is the VCC side. The other side is GND.
Switching the VCC and GND contacts does not do any harm (yes I tried it) as they are leds. But they just do not work when the power is not connected right.

Current delimiting resistor.

As with all leds these need a current delimiting resistor. More on that later.

Power

On our favorite Chinese website there are several suppliers of these filaments. So I scrolled through a lot of them to find some good specifications.


Most of them stated that the maximum current that could be supplied was 100ma at 3V.

The problem is that no microcontroller that I own can supply that much power from it's IO pins. The Raspberry Pi Pico for example can supply 4ma per IO pin and a maximum of 50ma on all IO pins combined. So the current needed for this filament would fry it. The ESP32 can roll out 40ma maximum at a single IO pin but the total (maximum sum of all IO pins) it can supply is 120Ma. Next to that a prolonged delivery of 40ma on a single pin will overheat and damage the chip. Therefore powering the led filament from a microcontroller is no option.

So I am going to supply the power from a 5V USB phone adapter.
That is 5V so we need to reduce that to 3V. The easiest way to do that is by using a resistor.

To calculate what value the resistor should have we need the following formula.

voltage supplied - voltage desired
-------------------------------------------------------
                  milliamps


The voltage supplied = 5V
The voltage desired  = 3V
The milliamps we need is 50 ma

As you can see I am not powering the strip to the maximum 100Ma.
This gives the following formula:

5V - 3V
----------
50 ma


And 50ma is 0.050 Amps
So that gives 2V / 0,050A is 40 and that makes the value of the resistor 40 Ohm.

So the minimal resistor value is 40 Ohm
Any higher value will do.

Supplying the power.

It is obvious that our ESP8266, ESP32 or Raspberry Pi Pico can never supply such power. So we need an external component for that: a transistor.

The most common used transistor for supplying power is the BC547. This transistor can supply a maximum of 100ma. So we could attach a maximum of 2 led filament strips to 1 of these BC547's. The BC547 is dead cheap. You can get about 50 pieces for 1 USD if you shop around.

If you need more current then use the 2N2222. This transistor is pin compatible with the BC547 but capable of delivering 800ma. Beware that this is under ideal conditions. It would need cooling to achieve that much current. So aim for a lower current like 200-400ma maximum.

I opted for the 2N2222

In real life.

In real life I did some experiments and found that a 100ohm resistor already supplied so much current that the ledstrip was very bright and could even be seen in broad daylight.

Let me do the recalculations for you.

5V - 3V
----------
 X ma


This should equal 100 Ohm

So 5-3 / X ma = 100 this gives 2 / 100 = 0.02A = 20 Ma.

Using a 2N2222 transistor I should be able to attach easily 10 led filaments (10 x 20ma - 200ma) to 1 IO pin of the microcontroller.

There is one extra calculation and that is the calculation for the resistor that needs to be attached to the base of the 2N2222 and that defines how much the output will be. I will not bother you with these calculations. But if you mail me I will show these to you in a separate story on this weblog.

The combined calculations.

I will not bother you with the calculations for the base resistor (until you mail me and ask for it).  I needed a resistor of 1.5K to 2K for the base of the transistor and for each led filament a resistor of 100 ohm.

2N2222

The 2N2222 has 3 connections E, B and C which are emitter, base and collector.


The Base is connected to a 1.5K resistor and that determines (the gain) the amount of current that will flow from the Emitter (GND) to the Collector. Any load (like our led filament) gets connected to the collector.
So the GND of the led filament is connected to the collector using the current delimiting resistor of 100Ohm.
The Emitter is connected to GND.

I made a test setup first without using a Microcontroller.

Breadboard for the first test.

Here is the breadboard for the first test.


The setup is the same as discussed above.
A resistor of 1.5K is attached to the base of the 2N2222. It is connected to a pushbutton that is connected to VCC. When the pushbutton is pressed some current will flow to the base of the transistor.
The Emitter is just connected to GND.
The Collector is connected through a 100 Ohm current delimiting resistor and from that to GND of the led filament. The VCC of the filament is connected to VCC.
The project is powered by a USB wall adapter, the kind used for phones.

When you push the button, the led filament will light up.
If the filament does not light up, check your connections. Most likely the VCC and GND of the filament have been swapped. Remember that the side where there is a whole in the connection is the VCC side.

That is all.

Attaching a Microcontroller.

The resistor connected to the base of the transistor is calculated at a value that accepts 3.3V from a microcontroller. We can use any microcontroller we like: ESP8266, ESP32 or Raspberry Pi Pico.

For our example we will be using the Raspberry Pi Pico.
The only thing we need to do for our first test is to attach a GPIO pin to the base resistor.
And of course connect the GND of the Pico to the rest of the project.

The Pico will get powered from our computer so:
DO NOT CONNECT THE VCC FROM THE USB POWER ADAPTER TO THE PICO'S POWER RAIL.
CONNECTING BOTH THE PICO WITH THE COMPUTER AND THE EXTRA USB POWER ADAPTER WILL BLAST YOUR PICO AND YOUR COMPUTER TO KINGDOM COME.



The first test program.

This is a small test program that puts the pin GPIO16 at HIGH, waits a second and then puts the pin LOW. The ledstrip will go on for a second, then off for a second and that sequence repeats.

# -------------------------------------------
# simple test for setting filament on and off
# -------------------------------------------

from machine import Pin
import time

filament = Pin(16, Pin.OUT)

while True:
    filament.value(1)
    time.sleep(1)
    filament.value(0)
    time.sleep(1)

Make sure this works before trying the next step.

Dimming the led filament

Now let's see if we can dim the filament with our program.
We can fake the voltage output of a GPIO pin by using PWM.

If you are not aware on how this works I can recommend my books on the Raspberry Pi Pico with MicroPython which you can find at the bottom of this story.

Here is the program that sends a PWM signal to GPIO16.
The led filament is off at first and then slowly gets brighter. When the maximum brightness has been reached it slowly dims till its off again and then the sequence repeats.

# -------------------------------------
# test program for dimming led filament
# with PWM
# -------------------------------------

import machine
import time

filament = machine.PWM(machine.Pin(16))
filament.freq(1000)

while True:
    min = 0
    max = 65536
    for i in range(min,max):
        filament.duty_u16(i)
        time.sleep(.00015)
    time.sleep(1)
    for i in range(max,min,-1):
        filament.duty_u16(i)
        time.sleep(.00015)
    time.sleep(1)

You can adjust the minimum value and the time between steps to your own liking.

Now everything works as expected we can start some real projects with this led filament.

So have fun,
Till next time

Luc Volders

Friday, May 9, 2025

Raspberry Pi pico with Bluetooth

For an index to all my stories click this text.

Some time ago I published a story on how to have the ESP32 communicate over bluetooth with your android Phone. Normally we use Wifi to communicate with the ESP32 but there might be circumstances in which you do not want that or can not use Wifi. You can re-read that story here: https://lucstechblog.blogspot.com/2024/08/esp32-bluetooth-classic-part-2.html

As you might know I wrote a book on the Raspberry Pi Pico which is a great microcontroller made by the Raspberry Pi Foundation. The Pico is not a linux computer but a Microcontroller like the Arduino's or ESP series. It has excellent features and a really good price-tag. A Pico can be had locally for around 5USD.  One of the readers of my book who is also a follower of this blog asked if it was possible for the Raspberry Pi Pico to communicate with his phone.
Well that is not directly possible as the Raspberry Pi Pico has no communication means on board. There is standard no Wifi or Bluetooth like the ESP8266 or ESP32 have.
There is however a solution available.

Back in 2016 as faithfull readers of this blog might remember I wrote a story on how to attach an ultrasonic distance meter to an Attiny85 and send it's data to a phone using a HC-06 bluetooth module. These modules are cheap and easy to get.

We can use that same module and attach it to a Raspberry Pi Pico to send data to your phone or receive data and commands from your phone. And that is exactly what we are going to do. You can use a HC-05 or HC-06 for this. Just use that what is most easily available.

The story written in 2016 about the Attiny 85 used C++ (Arduino language). For the Raspberry Pi Pico I am going to write a program in MicroPython.
The Phone App is written in MIT's App Inventor. You can find App Inventor here: https://appinventor.mit.edu/  

App Inventor has been featured in several stories and tutorials on this weblog which you can find here: http://lucstechblog.blogspot.com/p/index-of-my-stories.html

Bluetooth communication.

The Pico has so called UART connections. An UART is a, in this case build-in, hardware module that converts the data into a serial stream. The Pico has two UART's build in UART0 and UART1. I am using UART0 in this case.

To have the Pico communicate over bluetooth we need to attach a Bluetooth adapter that can convert the electrical signals from the Pico into a wireless transmission. The module often used for that is the HC-05 or the HC-06. So the HC-06 is connected to the internal UART. The breadboard setup shows how it is done.

The hardware setup



The Raspberry Pi Pico has ample I/O pins so for this test I am using 2 I/O pins for the communication with the HC-06 bluetooth module, and I use 4 I/O pins to attach leds.


The HC-06 is attached to the GND (pin 38) and 3V3-OUT (pin 36) for getting it's power. The TX line of the HC-06 is attached to the RX line (pin 2) of the Pico. The RX line of the HC-06 is attached to the TX line (pin 1) of the Raspberry Pi Pico. So the RX and TX lines are crossed. This makes sense. The Pico sends (TX) data to the Bluetooth module that receives that data (RX) and the other way round.

Next the 4 leds are connected to GND using a current delimiting resistor of 220Ohm and connected to the Pico's pins 15, 16, 17 and 19 which are GPIO 14, 13, 12 and 11.

Last year I made a Raspberry Pi Pico pin layout which you can print and lay on top of the Raspberry Pi Pico for easy reference. You can find that here: http://lucstechblog.blogspot.com/2021/03/raspberry-pico-pin-layout-help.html

The App

To have your Pico and Phone talk to eachother you need an App on your phone. The App I am going to show how to develop is for an Android Phone. As App Inventor nowadays also can be used to develop Apple app's you could adapt the program for an Iphone. However you're on your own here as I do not own an IPhone and am not willing to buy one due to it being terribly overpriced.

Unfortunately I do not have the space and time here to give you a complete App Inventor course. App Inventor does not have a steep learning curve and spending some hours with the program should get you going. Next to that there are several tutorials and examples on this weblog and App Inventor itself offers several tutorials. Check those here http://lucstechblog.blogspot.com/p/index-of-my-stories.html and here: https://appinventor.mit.edu/



This is how the screen will look eventually. At the top there is a ListPicker used for picking the right Bluetooth device. Below that there is a Tablearrangement. Inside the TableArrangement I placed 8 buttons that send commands for setting the led on or off.
Below that you see an empty square. That is a label (Label2) in which texts received from the Raspberry Pi pico will be shown.
And below that Label is Label1 with a ridiculous copyright notice.

Arrange the items to your own linking and give them the looks and colors of your choice. Play with this to learn how to use App Inventor and it's possibilities and functions.

There are 3 non-visible components at the bottom of the designer screen. The first one is the BluetoothClient component, the second is the Notifier component and the last is the clock component. You can find these in the menu on the left side of the designer screen (not shown here).
Drag them into the phone simulation screen and they will be placed at the bottom like shown here.

Now switch over to the blocks section.


This is a picture of all the necesary blocks. It may look complicated but if you get the hang of it, it really isn't.


Start with a Listpicker and put all the names and Bluetooth adresses in the list.

When the ListPicker has all the Bluetooth data and the user presses (clicks) on the desired Bluetooth device connect to it. When connected set a message on the screen (with the notifier) that the connection has succeeded or failed.


Create a block for each button that sends a particular text over Bluetooth when the button is clicked.


Here you can see the blocks that are used to set the first led on or off. When Button1 is clicked first the Bluetooth connection is tested. No use to try to send a command over Bluetooth when the device is not connected...
Then we send on1# if Button1 is pressed or off1# when Button2 is pressed.
The # is attached to the text as a means for the Pico's Python program to detect if receiving the message is completed.

The blocks for Button3 to Button8 are identical and only differ in the message they are going to send.



This last block uses the clock function to check every second wether there is a message received over Bluetooth. If a message is received it is displayed in Label2.

And that completes the App for your phone.

If you do not want to compose all the blocks yourself I have a copy of the above code available for you to download here:

https://www.mediafire.com/file/554kyre473onlh0/Pico_bluet_SR.aia/file

The Python program for the Pico.

Now we have a complete app available that can send and receive data over Bluetooth let's look at the Pico side. Here is the complete MicroPython program.

import os
import utime
from machine import UART, Pin

#print sys info
print(os.uname())

L1 = Pin(14,Pin.OUT)
L2 = Pin(13,Pin.OUT)
L3 = Pin(12,Pin.OUT)
L4 = Pin(11,Pin.OUT)

#indicate program started visually
led_onboard = machine.Pin(25, machine.Pin.OUT)
led_onboard.value(1)     # onboard LED ON for 0.5 sec
utime.sleep(0.5)
led_onboard.value(0)     # onboard LED ON for 0.5 sec

uart = machine.UART(0, baudrate=9600,
                    bits=8, parity=None, stop=1)
print(uart)

uart.write("hello")
utime.sleep(1)
uart.write("The temperature is : ")
uart.write(str(35))

L1.value(1)
utime.sleep(1)
L1.value(0)

L2.value(1)
utime.sleep(1)
L2.value(0)

L3.value(1)
utime.sleep(1)
L3.value(0)

L4.value(1)
utime.sleep(1)
L4.value(0)

while True:
        b=""
        a=""
        c=""
        while a != "#" :
           a = uart.read(1)
           c = (a.decode('utf-8'))
           a = c
           if (c != "#"):
             b = b + c
             print("received chars: ",len(b)," = ",b)
            
        if "on1" in b:
            L1.value(1)
            uart.write("Led 1 On")
        if "off1" in b:
            L1.value(0)
            uart.write("Led 1 Off")
            
        if "on2" in b:
            L2.value(1)
            uart.write("Led 2 On")
        if "off2" in b:
            L2.value(0)
            uart.write("Led 2 Off")

        if "on3" in b:
            L3.value(1)
            uart.write("Led 3 On")
        if "off3" in b:
            L3.value(0)
            uart.write("Led 3 Off")
            
        if "on4" in b:
            L4.value(1)
            uart.write("Led 4 On")
        if "off4" in b:
            L4.value(0)
            uart.write("Led 4 Off")


As usual I will show how this program works by highlighting certain parts.

import os
import utime
from machine import UART, Pin

First we import all the necesary libraries.

L1 = Pin(14,Pin.OUT)
L2 = Pin(13,Pin.OUT)
L3 = Pin(12,Pin.OUT)
L4 = Pin(11,Pin.OUT)

The four led's are defined and the I/O pins where they are attached to are set as OUTput.

#indicate program started visually
led_onboard = machine.Pin(25, machine.Pin.OUT)
led_onboard.value(1)     # onboard LED ON for 0.5 sec
utime.sleep(0.5)
led_onboard.value(0)     # onboard LED ON for 0.5 sec

Then we blink the Pico's internal led as an indication that the program has started.

uart = machine.UART(0, baudrate=9600,
                    bits=8, parity=None, stop=1)
print(uart)

These lines define the serial communication's settings and display them in Thonny's shell when the program runs.

uart.write("hello")
utime.sleep(1)
uart.write("The temperature is : ")
uart.write(str(35))


This is a test in which we send a fake message to the phone. First the word "hello"is send and next "The temperature is 35" As you can see the last line converts the number 35 into a string that can be send over Bluetooth.

L1.value(1)
utime.sleep(1)
L1.value(0)

L2.value(1)
utime.sleep(1)
L2.value(0)

L3.value(1)
utime.sleep(1)
L3.value(0)

L4.value(1)
utime.sleep(1)
L4.value(0)


The leds are all set on and then off as a test so we can see they are working and attached to the right I/O pins.

Then comes the most important part of the program. It is where an incoming message from the Phone is read and decoded.

while True:
        b=""
        a=""
        c=""
        while a != "#" :
           a = uart.read(1)
           c = (a.decode('utf-8'))

The program starts with defining some helper variables and making them empty.
Next a test is done wether the variable a comntains "#" if that is the case it is the last character received and it should be ignored.

If it is not "#" the next character is read from the uart and decoded to a normal ascii value.

           a = c
           if (c != "#"):
             b = b + c
             print("received chars: ",len(b)," = ",b)

Again we test wether the received character is "#" and if not the character is added to the variable b. Then we print in the shell the received characters and the length of the total received characters. This is just a test to see if everything send from the phone is indeed received.

        if "on1" in b:
            L1.value(1)
            uart.write("Led 1 On")
        if "off1" in b:
            L1.value(0)
            uart.write("Led 1 Off")

When "#" is received the command is now complete and stored in the variable b. Now we can test which command is received and set the appropriate led on or off.
Then we send "Led 1 On" or "Led 1 Off" back to the phone so the phone can check if the command really has been received and the command is executed.
These lines are repeated for all 4 leds.

In the field

When te App is started firrst press on the field with the text "Pick your Bluetooth device" and make sure Bluetooth is activated on your phone.



A new screen opens showing all Bluetooth devices available and the ones that you have used in the past. The top one has the Bluetooth adress and name (HC-06) we want to connect to. Press on that line and the connection is made.

Now you can use the app and switch the led's on and off.

This clearly demonstrates how to receive data from Bluetooth and how to send data over Bluetooth.

Of course this is just a framework to send and receive all kinds of data from your phone to the Raspberry Pico and the other way round.

For more detailed info on the Raspberry Pi Pico, a short course on MicroPython and using all kinds of sensors with the Pico please buy my book Raspberry Pi Pico Simplified: https://lucstechblog.blogspot.com/2021/08/raspberry-pi-pico-simplified-now.html

Till next time
have fun

Luc Volders








Friday, April 14, 2023

Showerclock with Raspberry Pi Pico and MicroPython

For an index to all my stories click this text

So we have an energy crisis. And in the Netherlands we all are advised to reduce our energy use. One thing we are asked to do is to take shorter showers. Preferably maximum 15 minutes. And as docile citizens we do as asked..................Wrong !!! We indeed shorten our shower time not because the Government asks us but because energy prices have more then tripled last year.

I have no clock in my shower. And I stopped wearing wristwatches ages ago. So how am I going to know if 15 minutes have past.

Well if you call yourself a tinkerer you should live up to it. Let's build a shower clock !!

My requirements

I do not want a mechanical clock with hands. I want an electronic one. Next to that a small display is not convenient. And it should be watertight.

So then I got an idea.

The time is fixed. So I do not have to have buttons to adjust it. And a small display is an annoyance in the shower so I don't need that either. So why not have a bunch of leds that give a color code. Green when you start showering and red when your 15 minutes are spend.

To have a visual indication on how much time is left I decided to decrease the number of leds that were lit according to the time that has passed. In reality this means that the more time has passed the more the amount of light will get dimmed.

The easiest way to achive this is by using a neopixel ring.

To control the neopixel ring I used a Raspberry Pi Pico. The Pico is widely available and cheap. Next to that it's prime programming language is MicroPython and I love MicroPython.

If you want to learn more about MicroPython on the Raspberry Pi Pico consider buying one of my books on this subject:

http://lucstechblog.blogspot.com/2021/08/raspberry-pi-pico-simplified-now.html

or

http://lucstechblog.blogspot.com/2022/10/pico-w-simplified-released.html

The books are available on Amazon worldwide and through every major bookstore.

The breadboard setup.

If you do not know what neopixels are you really have been living under a rock the last few years, or just stepped into this hobby. In 2015 I already wrote a general introduction to these adressable leds. You can find it here : http://lucstechblog.blogspot.com/2015/10/neopixels-ws2812-intro.html

Neopixels are attached to a microcontroller through a 300-500 ohm resistor. Choose whatever you have available. I usually use a 470Ohm resistor. Next to that attach a large capacitor (1000 microfahrad at 6.3 volts or higher) across the GND and + of the power supply.

That is all.

I had a Neopixel ring of 12 leds and attached it (through the resistor) to GPIO pin 5 of the Raspberry Pi Pico.




Wokwi simulation.

Last year I wrote a story on Wokwi. Wokwi runs on your PC. You can build a circuit in it, add some code and do a real time simulation. You can test circuits with ESP32 or Raspberry Pi Pico and test code in C++ (arduino language) or MicroPython.

If you want to know more just read my story on Wokwi:
http://lucstechblog.blogspot.com/2022/04/simulate-your-controllers-with-wokwi.html

So I thought to put it to the test. And it works flawlessly.
Just point your browser to:

https://wokwi.com/projects/360215418797037569

and you can test the shower timer !!!


 

Here is how it looks in your browser.

In the simulation I have set the time to .2 minutes. That is 12 seconds so the Neopixels will go out one by one each second. Just adjust this line. Press the green button (where the red arrow points) to start the simulation.

minutes = .2

To any other number of minutes.

Wokwi is a great tool to test your prototypes before building them on a breadboard. There are still some components missing (like capacitors) but it is getting better all the time.

There is just one small problem. In the simulator this program automatically runs twice. In real time it stops after the 15 minutes. I had a conversation with the Wokwi team and they are working on that problem, so maybe it is even solved when you are reading this. So don't worry in real time it works as it should.

The program

For this program I used the simple Neoppixel driver which is included with MicroPython for the Pico. There is a far better library with much more functionality which I described in my book Raspberry Pi Pico Simplified. But that is overkill for this project.

Next to that we only need the machine library to control the IO pin and the utime library to get the clock working. Both libraries are included in the MicroPython distribution.

Here is the full program.

'''
Shower Timer with a fixed time of 16 minutes
http://lucstechblog.blogspot.com
'''

import machine
import neopixel
import utime

# Set up the neopixels
numpixels = 12
pixelpin = machine.Pin(5)
neopixels = neopixel.NeoPixel(pixelpin, numpixels)

# Set up the timer
minutes = 16
seconds = minutes * 60


print("Starting timer")
for i in range(numpixels):
    neopixels[i] = (0, 255, 0)  # Set all neopixels to green
neopixels.write()

while seconds > 0:
    # Update the neopixels
    remaining = seconds / (minutes * 60)
    num_lit = int(numpixels * remaining)
    for i in range(numpixels):
        if i < num_lit:
            neopixels[i] = (0, 255, 0)  # Set lit neopixels to green
        else:
            neopixels[i] = (0, 0, 0)  # Set unlit neopixels to black
    neopixels.write()
    # Decrement the timer
    seconds -= 1
    utime.sleep(1)
    
print("Timer finished")
for i in range(numpixels):
    neopixels[i] = (255, 0, 0)  # Set all neopixels to red
neopixels.write()

As you can see this is a straight forward program. No functions and no endless loops that keep the program running.

As usual on this weblog I describe some parts of the program in detail.

import machine
import neopixel
import utime

# Set up the neopixels
numpixels = 12
pixelpin = machine.Pin(5)
neopixels = neopixel.NeoPixel(pixelpin, numpixels)

# Set up the timer
minutes = 16
seconds = minutes * 60

Nothing special in the above part. The required libraries are loaded and the Neopixel ring with 12 pixels is attached to GP5. The number of minutes is set to 16 which I will explain later. The number of minutes are used to calculate the number of seconds.

print("Starting timer")
for i in range(numpixels):
    neopixels[i] = (0, 255, 0)  # Set all neopixels to green
neopixels.write()

For debugging purposes I added some print statements that will do nothing in the final setup. All 12 neopixels are set to green.

while seconds > 0:

Here starts the program. It runs as long as the number of seconds is larger as 0

    remaining = seconds / (minutes * 60)
    num_lit = int(numpixels * remaining)

This is where the calculation is made on how many Neopixels must be lit and how many are black.

Let us say 3 minutes have passed.
When the program starts the number of seconds = 16 minutes x 60 seconds = 960.
When 3 minutes have past 3 x 60 = 180 seconds have passed. This means that there are 960 - 180 = 780 seconds left.

    remaining = (780) / (16 x 60)
    remaining = 0,812

    num_lit = int(12 * 0,812)
    num_lit = int(9,75) = 9


So after 3 minutes 9 Neopixels will be lit and 3 will be dark.

    for i in range(numpixels):
        if i < num_lit:
            neopixels[i] = (0, 255, 0)  # Set lit neopixels to green
        else:
            neopixels[i] = (0, 0, 0)  # Set unlit neopixels to black
    neopixels.write()

This is where the Neopixels are set to green or black according to the calculation just made.

    # Decrement the timer
    seconds -= 1
    utime.sleep(1)

And in this last part the program stops for a second and the number of seconds remaining is decreased by one.

print("Timer finished")
for i in range(numpixels):
    neopixels[i] = (255, 0, 0)  # Set all neopixels to red
neopixels.write()

When the number of remaining seconds reaches 0 all Neopixels are set to red as a warning that the time is up !!!

Why 16 minutes and not 15

Well I took the easy way out. I attached the Pico to a powerbank. So to start the program just plug the powerbank in and the program runs. No need for an on/off switch or a start button.

But then I need some time to put the cable into the watertight housing and screw the lid on, and then step into the shower This takes not much time but I decided to reserve a minute for this. That is why I opted for 16 minutes in stead of 15.

If you build this for  yourself and need more or less time just adjust the line:

minutes = 16

In real life.

Save the program on your Pico and call it main.py. A program with the name main.py will automatically start when the Pico is powered up.



Here is the picture of the breadboard setup with my attached powerbank in the back.



Here you can see how it is all crammed into a jar. I did some testing and the jar is watertight. I did not test for waterproof as I am not going to submerge this into my bath. It is standing on a shelf in my bathroom and for that is is sufficient watertight.



And here is how it looks when the time is up !!!

Last and next thing to do is to put it on a stripboard for permanent use.

Other purposes.

Well this timer can be used for many other purposes. How about a timer for tasks behind your desk. Set the time to 30 minutes and after these you stop whatever you're doing and start something else.

Till next time,
Have fun

Luc Volders