Analog Clock circuit by flashing LEDs

Hey guys,

I am working on a rather hefty project for a gift. It is an "analog" clock that is powered by Arduino UNO R2. I have a lot of aspects that will need guidance as this is my first project with Arduino. I have done other circuit modules before when I was in electrical engineering I know some coding (Mainly MatLab code), but am relatively new to C. I have a good understanding of the logic however. Anyways Here is a picture of the prototype drawing. everything that is black is going to be backlit with LED's. There will be minute and hour hands powered by a stepper motor. There will also be a 7-segment display at the bottom (not shown).

Now here is where I need guidance. there are a lot of features I want to design for this clock. I will list them out

  • I want each second tick-mark to blink for the second and fading out, leading up to the tick-mark. lets say the backlit brightness is 10% of the set max. when it blinks, it will go to 100% and fade back to 10%.
  • Using the same percent brightness scale, I want the numbers 1-12 to have a slow fade based on where the hour hand is: if its on 1, the 1 will light to 100%. if its on 2, the 2 will light at 100% and the 1 will be back at 10%. if its at 1:30, both the 1 and the 2 will be at 10+(100-10)/2 = 55%. if its at 1:10, the 1 will light at 10+(100-10)(5/6)=85% and the 2 will light at 10+(100-10)(1/6)=25% (formula is min + (max - min)*(minutes/60) This won't be too hard I don't think, but that remains to be seen.
  • I want the hands to automatically go to the correct time when powered on from the wall plug. As I understand, I can keep the time on the arduino board, and plan on using a sensor of some sort to set the hands to a "home" position, and then rotate to the correct time. Also, these hands will be made of acrylic and have another LED shone trough them. I'm not concerned about that right now.
  • Lastly, every hour on the hour, I want the whole clock that is backlit to glow and fade the number of times that the hour shows. so for 3:00, itll blink/glow 3 times. This isn't as important, but I thought it'd be cool.

Ok so that's a hefty list of features. I didn't even go into the gearing of the hands or how to get them to light up. Right now I'm focused more on the other lighting and coding. I know this list is too big to cover at the same time, so I have a few questions and want to focus on the first point only for right now.

Ok so first feasibility question: I want this clock to be accurate for a long time, years if possible. I know I'll be able to code everything I want independently: I am concerned for the time it takes to do loops and whatnot however, and how it will deviate from true time. I saw that Arduino keeps time on the board: is there a way to code a reset button for the lights and hands at the end of the day for instance to recall the true time, and adjust accordingly? If not, are there any general guidelines to keep computational time to a minimum?

Next, I have made a code for fading of the second lights already here:

*
6 LED clock

 this code does a blink and fade for 6 LED's per second in order.

 */

int led3 = 3;           // the pin that the LED is attached to
int led5 = 5;
int led6 = 6;
int led9 = 9;
int led10 = 10;
int led11 = 11;
int brightness = 255;    // how bright the LED is
int loopbrightness = 255;  // initial brightness value for the loops
int fadeAmount = 5;    // how many points to fade the LED by


void setup() {               
  // initialize the digital pin as an output.
  pinMode(led3, OUTPUT);
  analogWrite(led3, 10);
  pinMode(led5, OUTPUT);
  analogWrite(led5, 10);
  pinMode(led6, OUTPUT);
  analogWrite(led6, 10);
  pinMode(led9, OUTPUT);
  analogWrite(led9, 10);
  pinMode(led10, OUTPUT);
  analogWrite(led10, 10);
  pinMode(led11, OUTPUT); 
  analogWrite(led11, 10);
}


void loop() {

    {loopbrightness = brightness; //starts at 255
     do {
      analogWrite(led3, loopbrightness);   // turn the LED on, starting at 255)
      loopbrightness = loopbrightness - fadeAmount; //decreases the brightness by 5
      delay(15); // each step takes .015 seconds: there are (255-10)/5 = 49 iterations, for a total of .735 seconds, with an extra .015 added at the end.
    }while(loopbrightness > 5); } //breaks when loopbrightness <=5. the ending written loopbrightness is 10, and .015 seconds is delayed after the subtraction, as stated above.
    delay(250); //finish off the remaining time in a second: 1-.735-.015 = .250
    
    {loopbrightness = brightness; //second LED
     do {
      analogWrite(led5, loopbrightness);   
      loopbrightness = loopbrightness - fadeAmount;
      delay(15); 
    }while(loopbrightness > 5); }
    delay(250); 
    
    {loopbrightness = brightness; //third LED
     do {
      analogWrite(led6, loopbrightness);   
      loopbrightness = loopbrightness - fadeAmount;
      delay(15); 
    }while(loopbrightness > 5); } 
    delay(250); 
    
    {loopbrightness = brightness; //fourth LED
     do {
      analogWrite(led9, loopbrightness);   
      loopbrightness = loopbrightness - fadeAmount;
      delay(15); 
    }while(loopbrightness > 5); } 
    delay(250); 
    
    {loopbrightness = brightness; //fifth LED
     do {
      analogWrite(led10, loopbrightness);   
      loopbrightness = loopbrightness - fadeAmount;
      delay(15); 
    }while(loopbrightness > 5); } 
    delay(250); 
    
    {loopbrightness = brightness; //sixth LED
     do {
      analogWrite(led11, loopbrightness);   
      loopbrightness = loopbrightness - fadeAmount;
      delay(15); 
    }while(loopbrightness > 5); } 
    delay(250); 
    
}

This works with only 6 LEDs since the board I am working on only has 6 digital pins that can do analog. I was suggested that to do all 60 seconds, I could use the serial ports on the board to an LED driver or multiplexer or something. I have this driver IC that I thought might work, but with my fade effect, I think this only does a digital output, not an analog signal that I need. Does anyone have ideas for this? I considered getting a bigger Arduino, but I want to see if I can make this work first.

Lastly for now, along the same lines, the circuit I made earlier tonight has all LEDs hooked up to power with 2 resistors that go 680 ohm to 330 ohm to LED to ground. here is a picture:

For the blink fading effect I want, I thought I could send the signal from the arduino between the resistors and work: but when I power the Arduino, the LED goes dark and I have to set a minimum brightness (reflected in the code). Any other ideas for the circuit? should I stay with the idea of all the LEDs connected to ground, and sending a signal that will make individual ones brighter? or try something altogether different?

Edit: could I possibly do the fade effect with capacitors, and not even worry about analog signals, using only digital signals? This might solve so many of my problems.

Anyways lots of questions, I know this is a lot. Thanks for reading the wall of text and your replies. I appreciate them.

Look into WS2812B for the LEDs.

Well now that is a cool LED setup. Unfortunately the diameter is smaller than what I want, I'm looking at about double that. Otherwise it'd be perfect!

Hi There!

Looks like a pretty fun project you have going on.

Some ideas for you:
-The best way to get an accurate time source, I'd recommend using a real time clock (RTC). The Arduino UNO R2 doesn't have one built in, but you can get some nice add on boards from sparkfun or adafruit. This way your code has no impact on the accuracy of your time keeping.

-You're on the right track dimming the LEDs via PWM.
To drive more than the 6 LEDs you have pins for you can look into:

  1. Charliplexing, with your 6 pins you could drive up to 30 leds. Dimming may be an issue here.
  2. Binary Code Modulation, allows you to use all 14 of your outputs to dim leds.
  3. Port Expander, to have a dedicated pin for each led.
  4. LED driver (there are ways to dim using these, or you might be able to find with with dim functionality).

Hope this helps.
-Adam

Forgot to say:

The way you have your LEDs hooked up looks a little strange to me.

You can source directly from your arduino pin. See attached.

To figure out your resistor value:

First find out your IO voltage, should be 5V for your board.
Next find the forward voltage for your LED (look in the datasheet), for Red it's usually ~2V.
Determine the current you want (determines brightness). For standard LEDs they usually test up to ~20mA, but for a system that will use as many LEDs as yours, you most likely want to dial that down to 5mA or less.

Use Ohms law and play with the values to determine the max brightness you want:

(supply voltage - LED forward voltage) / current = resistance

ex:
for 10mA, (5V - 2V) / .010 = 300 ohms

adhue587:
The way you have your LEDs hooked up looks a little strange to me.

That was simply a way of giving the LEDs the "10%" minimum glow.

If you didn't want the different brightness, a MAX7219 is the way to drive 60 (64) LEDs. You can still do two brightness levels by dynamically changing the pattern in the MAX7219 between all LEDs lit and only one, but it would be difficult to fade.

You want the NeoPixels (WS2812). You are just going to have to assemble them by hand or CAD a custom PCB to mount them. And of course, they are coloured.