Saturday, June 3, 2017, 10:08
Posted by Administrator
Posted by Administrator
I was inspired by this post on Hackaday where Setvir created an elegant single LED clock using an Arduino Pro Micro. I'm more of a fan of Raspberry Pi's and have been wanting to come up with a simple single LED clock design of my own ever since reading that article.
It occurred to me today that the Pi Zero's status light could be commandeered for this purpose which would eliminate the need for any other additional parts. I considered using Python for this but eventually decided upon using bash scripting as it is already baked in to most common Pi Zero images such as Raspbian or Retropie.
The heavily commented code below is what I came up with to bring this clock design to life:
#!/bin/bash
# A minimalist led clock for the Pi Zero ( And Pi Zero W )
# Uses only the onboard led to blink the current hour
# And then to quickly blink the the tens place of minutes
#
# Spike Snell - June 2017
# First set the trigger for the onboard green led to none so that we have control of it
echo none | sudo tee /sys/class/leds/led0/trigger
# Turn off the led so that we have a known state to start
echo 1 | sudo tee /sys/class/leds/led0/brightness
# Loop endlessly
while true
do
# Wait for a full second to seperate our visual count
sleep 1
# Loop for the current number of hours
# Trim leading 0's so that 08 and 09 aren't interpreted as an octal base
for ((n=0;n<$(date +"%I" | sed 's/^0*//');n++))
do
# Wait a bit
sleep .25
# Turn the led on
echo 0 | sudo tee /sys/class/leds/led0/brightness
# Wait a bit
sleep .25
# Turn the led off
echo 1 | sudo tee /sys/class/leds/led0/brightness
done
# Wait for a half second
sleep .5
# Now loop very quickly for the current first tens digit of minutes
for ((n=0;n<$(date +"%M" | head -c 1);n++))
do
# Wait for a small bit
sleep .1
# Turn the led on
echo 0 | sudo tee /sys/class/leds/led0/brightness
# Wait for a small bit
sleep .1
# Turn the led off
echo 1 | sudo tee /sys/class/leds/led0/brightness
done
done
I saved the script in my home directory as minimalistClock.sh and made it executable with the command chmod +x minimalClock.sh. It is then run under its own screen with the command screen ./minimalClock.sh since it can be somewhat distracting if running on your main session due to all the echo commands. Screen is also a very handy way to make the clock persist and keep running even after logging out of your current session. Use the command sudo apt-get install screen if you don't already have it installed. After getting the clock up and running in screen you can press ctrl+a then ctrl+d to detach the screen and have it keep running in the background.
The clock is read by counting the number of long blinks to get the hours, and then the number of short blinks to get the tenth place of minutes. For example, 4 longer blinks followed by 3 shorter blinks indicates that the current time is about 4:30. I decided not to use a 24 hour military format for this because that would end up being far too many blinks when late in the day.
This script can be easily added to /etc/rc.local so that it will begin running when the Pi starts up.
This is exactly the minimalistic clock that I have been desiring for so long and it looks great running in my translucent Pi Zero Tic Tac Case which I have attached to the wall. Let me know what you all think of this basic clock design. If it could be made even simpler in some way I would love to experiment with that.
-- Edit March 2018
I have edited the code slighty in order for it to work on a Raspberry Pi 3 B+ ( Probably the non-plus version as well ).
Same idea as before, but this time using the onboard status led on the back of the bigger Pi. For some reason the light/dark values all had to be reversed, but other than that the code is the same. I found that putting a piece of electrical tape over the red led ( Which is always on when the Pi is receiving power ) makes the clock a lot more pleasant to leave on.
#!/bin/bash
# A minimalist led clock for the Pi 3 B+
# Uses only the onboard green led to blink the current hour
# And then to quickly blink the the tens place of minutes
#
# Spike Snell - March 2018
# First set the trigger for the onboard green led to none so that we have control of it
echo none | sudo tee /sys/class/leds/led0/trigger
# Turn off the led so that we have a known state to start
echo 0 | sudo tee /sys/class/leds/led0/brightness
# Loop endlessly
while true
do
# Wait for a full second to seperate our visual count
sleep 1
# Loop for the current number of hours
# Trim leading 0's so that 08 and 09 aren't interpreted as an octal base
for ((n=0;n<$(date +"%I" | sed 's/^0*//');n++))
do
# Wait a bit
sleep .25
# Turn the led on
echo 1 | sudo tee /sys/class/leds/led0/brightness
# Wait a bit
sleep .25
# Turn the led off
echo 0 | sudo tee /sys/class/leds/led0/brightness
done
# Wait for a half second
sleep .5
# Now loop very quickly for the current first tens digit of minutes
for ((n=0;n<$(date +"%M" | head -c 1);n++))
do
# Wait for a small bit
sleep .2
# Turn the led on
echo 1 | sudo tee /sys/class/leds/led0/brightness
# Wait for a small bit
sleep .2
# Turn the led off
echo 0 | sudo tee /sys/class/leds/led0/brightness
done
done
2 comments
( 39855 views )
| permalink
| ( 3 / 5934 )