Binary Clock, Part 2

Atoms, Electrons, Photons
[vc_row][vc_column][vc_column_text]

The long awaited part 2 of this blog post has finally arrived! Though I’ve been tinkering on this project for the past two years, I decided to write it up to coincide with the outrageous arrest of 14 year old tinkerer Ahmed Mohamed who was hand cuffed because his teacher thought his electronic clock project looked like a bomb. This binary clock project of mine ended up being a personal electronic circuit design introduction course. You can download all the relevant files here if you want to make your own.<br />I forgot what the original inspiration was but what I’m ending up with is a working binary clock on a custom printed circuit board. Ultimately, this project will involve fiber optics inside polished cement for a unique time piece but we’re not there yet… Here’s what I learned so far:

Bit shifters

A binary clock needs 20 individual blinky things and the arduino has less than that, so I needed to figure out a way to create more individually addressable outputs. The solution I found is a the 74HC595N chip that can turn two inputs into 8. In fact, you can wire them in series and they can provide you with any number of outputs in multiples of 8. I decided to use one to drive the hours display, one for the minutes, and one for the seconds.

There are tons of tutorials for them so it was fairly straight forward to get it working.

Keeping time

While you can make a timer with just an arduino, it is not very accurate and it has no way to keep the clock going if you unplug the power. I used a rtc1307 chip which is designed for just this purpose. It keeps time accurately and uses a small battery to continue keeping time when the power is disconnected.

Again, there is an arduino library available and a good amount of tutorials out there so it wasn’t too hard to test it and incorporate it in the build.

Removing the arduino

Eventually, since I wanted to end up with a single circuit board, I didn’t want to have to plug anyting into an arduino. Once again, the internet is a wonderful resource which allowed me to figure out how put only the arduino components I needed onto a bread board. I can upload the code onto the chip by putting it on an arduino, and then pull it off of there to mount it directly onto the breadboard.

Code

The clock can be set to one of 4 modes: display time, set hours, set minutes or set seconds. There are two buttons. One button toggles between all the different modes, while the other increments the count of the hours, minutes, and seconds when they are in their respective mode.

#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h>

//Pin connected to ST_CP of 74HC595
const int latchPin = 8;
//Pin connected to SH_CP of 74HC595
const int clockPin = 12;
////Pin connected to DS of 74HC595
const int dataPin = 11;

//Pins for setting the time
const int button0Pin = 5;
const int button1Pin = 6;

// Variables for debounce
int button0State;
int button1State;
int previousButton0State = LOW;
int previousButton1State = LOW;

long lastDebounce0Time = 0; // the last time the output pin was toggled
long lastDebounce1Time = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
int button0Mode = 0;

// object to communicate with RTC
tmElements_t tm;

void setup() {
// Setup pins for the shift register
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);

// Setup pins for manual time setting
pinMode(button0Pin, INPUT);
pinMode(button1Pin, INPUT);
}

void loop() {

RTC.read(tm);
// button 0 can be in display time, hour set, minute set, and second set modes.
//
if( button0Mode == 0){
timeDisplayMode();
}
else if( button0Mode == 1){
setHourMode();
}
else if( button0Mode == 2){
setMinuteMode();
}
else if( button0Mode == 3){
setSecondMode();
}

//
// mode switching
//
// Just after a button is pushed or released, there is noise where the value returned is 0/1 random.
// debouncing consists of reading the incomming value and, if it has changed, storing the time the change was noticed.
// It then continues to check and if after a certain amount of time (delay), it reads the input value and it is still the changed value
// noticed before, then it means that an actual state change happened.
int reading0 = digitalRead(button0Pin);
// this only happens when the input value is different from the last time the value was read
if (reading0 != previousButton0State) {
lastDebounce0Time = millis();
}
// we only enter this loop if the returned value hasn’t changed in a while, which means we are not in the noisy transition
if ((millis() – lastDebounce0Time) > debounceDelay) {
// if we are in here, it means we got two similar readings.
if (reading0 != button0State) {
// if the two similar readings we got are different from the stored state, we must have changed
button0State = reading0;
if(reading0 == HIGH){
button0Mode = (button0Mode+1)%4;
}
}
}
previousButton0State = reading0;

delay(100);

}

void timeDisplayMode(){
int sc = tm.Second;
int mn = tm.Minute;
int hr = tm.Hour;
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, setTimeBits(sc,3));
shiftOut(dataPin, clockPin, MSBFIRST, setTimeBits(mn,3));
shiftOut(dataPin, clockPin, MSBFIRST, setTimeBits(hr,2));
digitalWrite(latchPin, HIGH);
}

void setHourMode(){
int reading1 = digitalRead(button1Pin);
if (reading1 != previousButton1State) {
// reset the debouncing timer
lastDebounce1Time = millis();
}
if ((millis() – lastDebounce1Time) > debounceDelay) {
if (reading1 != button1State) {
button1State = reading1;
if(reading1 == HIGH){
tm.Hour = (tm.Hour+1)%24;
RTC.write(tm);
}
}
}
previousButton1State = reading1;

digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, setTimeBits(0,3));
shiftOut(dataPin, clockPin, MSBFIRST, setTimeBits(0,3));
shiftOut(dataPin, clockPin, MSBFIRST, setTimeBits(tm.Hour,2));
digitalWrite(latchPin, HIGH);
}

void setMinuteMode(){
int reading1 = digitalRead(button1Pin);
if (reading1 != previousButton1State) {
// reset the debouncing timer
lastDebounce1Time = millis();
}
if ((millis() – lastDebounce1Time) > debounceDelay) {
if (reading1 != button1State) {
button1State = reading1;
if(reading1 == HIGH){
tm.Minute = (tm.Minute+1)%60;
RTC.write(tm);
}
}
}
previousButton1State = reading1;

digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, setTimeBits(0,3));
shiftOut(dataPin, clockPin, MSBFIRST, setTimeBits(tm.Minute,3));
shiftOut(dataPin, clockPin, MSBFIRST, setTimeBits(0,2));
digitalWrite(latchPin, HIGH);
}

void setSecondMode(){
int reading1 = digitalRead(button1Pin);
if (reading1 != previousButton1State) {
// reset the debouncing timer
lastDebounce1Time = millis();
}
if ((millis() – lastDebounce1Time) > debounceDelay) {
if (reading1 != button1State) {
button1State = reading1;
if(reading1 == HIGH){
tm.Second = (tm.Second+1)%60;
RTC.write(tm);
}
}
}
previousButton1State = reading1;

digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, setTimeBits(tm.Second,3));
shiftOut(dataPin, clockPin, MSBFIRST, setTimeBits(0,3));
shiftOut(dataPin, clockPin, MSBFIRST, setTimeBits(0,2));
digitalWrite(latchPin, HIGH);
}

// create binary value for each digit of the hour/minute/second number
// offset represents how many bits are used for the tens.
int setTimeBits(int n, int offset){
int n1 = n%10;
int n0 = (n-n1)/10;
return n0 | n1<<offset;
}

Schematic and board

Once the breadboard was working, I set out to sketch the circuit in Fritzing. While it’s not quite as intimidating as EAGLE cad, I ended up using the latter after running into some limitations with the former (I don’t remember what they were). There was a lot for me to learn there but, in the end, it’s conceptually pretty simple: all the pieces have to be connected together correctly. It’s just another way to represent the circuit. Once that was done, I started with the board. I laid out all the components and let the software automatically figure out how to create the correct traces.
One cool thing is that if you choose the correct electronic components in the software, all the size and shapes are properly represented when you are designing the board. It’s a huge pain in the ass to sort through all the libraries of components, though, specially when you don’t know what all the specs mean.

 

The eagle cad files are included in the download file at the top of this page.

Manufacturing the board

Super simple: just go online and find a service that will manufacture them. For this project, I used oshpark.com and dirtypcbs.com, which allow you to upload your designs right out of EAGLE cad. After a few weeks, you get your board in the mail, ready for you to solder the components on. I order my components from mouser.com, which allows you to save a collection of various components into a project specific list. Again, finding the right components amongst the tens of thousands they have available is really time consuming and annoying. But now, I have my parts list so I never have to go through that again if I want to solder up new versions of the board. The list of parts in included in the download file at the top of this page.

The ugly truth

If you were paying attention, you no doubt noticed in the preceding paragraph that I used two board manufacturers. That is because the first board layouts I had printed actually had shorts. I suppose it’s probably not that uncommon, but it’s really frustrating to upload your designs, order the boards, wait for them to be delivered, spend all this time soldering the board to find out it doesn’t work, and then it can be challenging to figure out where the wires are getting crossed. In the end, I spent about $150 on boards and parts that ended up not working. I guess that’s the cost of learning… My first two board designs were ordered through Oshpark, and the minimum order was 3, for about $50. The third order was done on dirtypcbs and was $25 for 10 boards. They feel cheaper and took forever to get delivered but you sure can’t beat the price.

 

Flashing Arduino bootloader with Teensy 2.0

Electrons

What

If, like me, you want to flash the arduino bootloader onto some blank ATMEGA328 chips on a breadboard and only have a teensy 2.0 on hand (because you’ve short circuited all the working ATMEGA chips you had and so can’t use your arduino), and, unlike me, you don’t have hours to spare trying all freaking possible software/hardware/wiring combination to get it working, here’s what worked for me. Also, since I didn’t want to bother with the crystal, capacitors and resistor, I went for the minimal setup.

Why

Because a chip with the bootloader already loaded costs $5 and a chip without costs $2. And also, really, just because…

How

  • I used arduino 1.0.6 on a macbook running osX 10.6 (For those running Yosemite, there are documented issues recognizing the USB port)
  • I downloaded Breadboard1-0-x.zip and installed the contained breadboard directory in the “hardware” folder of my sketches.
  • In arduino, load the ArduinoISP sketch from the examples, change the LED pin to 11 (#define LED_HB 11), select Teensy 2.0 as you board and upload the sketch
  • Wire up the ATMEGA as follows.
  • Relaunch arduino and for your board, select Tools > Boards > “ATmega328 on a breadboard (8 MHz internal clock)” and Tools > Programmer > “Arduino as ISP”
  • Select Tools > Burn Bootloader
  • Done!
  • This is basically the steps outlined in the official Arduino website, except for changing the LED pin to 11 in the ArduinoISP sketch and wiring the ATMEGA to the proper pins on the Teensy 2.0. (Note that you don’t even have to change the code to reflect the different pin numbers since they are referred to by their function rather than their number).

There

Here’s a picture and a schematic…

Self Illuminating Responsive Tintype Frame

Art, Atoms, Electrons

The area of a tintype image that gets exposed to the most light essentially becomes a pure silver coating. While the reflective properties of silver make the final image a bit darker than traditional photographic prints, it also gives it a really compelling silvery metallic reflective feel. As a way to highlight those unique qualities, I decided to experiment with hiding LEDs in a box frame to illuminate the image from inside. I also wanted to find a way to intensify the lighting and make the image come alive as viewers got close to it.

Framed!

I inherited some antique oak wood floor pieces and made quick work of it on the old chop saw.

I built some walls behind the front of the frame and attached some LED string lights on it.

I mounted an 8×10 tintype on a black backing board cut to fit at the bottom of the frame’s walls.

Lights OFF / Lights ON

Controls

The next step was to figure out the kind of sensor needed to make the lights come on as a viewer approached the frame. I figured the sensor required would need a range of at least a few meters and be able to return the specific distance to the closest obstructing object. I am not a distance sensor expert so I used the internet to help. In the end, I settled for a Maxsonar EZ0 LV. It’s cheap, it’s got a range of a few meters and it’s got both serial, analog and PWM outputs. I hooked it up to a teensy board and confirmed the sensor was returning appropriate distance values. On the lighting front, I was planning on controlling the brightness of the LED string with the teensy, but since the LED string requires a 12V supply and the teensy outputs only 5V, I used a MOSFET driven by the teensy’s output to modulate the LED’s power supply.

The electronically savvy amongst you will notice that I’m currently using 2 power plugs: a 12V one for the LED and a 5V USB supply for the teensy, which is stupid. I tried to use a voltage regulator to convert the 12V to a 5V supply but somehow it created much noisier reading on the distance sensor… I must have missed a capacitor somewhere. I will try using an Arduino Nano which can take a 12V supply directly.

Code

God, I love the internet!!! I read somewhere that the sensor’s PWM signal was much cleaner so I started with that but I eventually found out that it also is much much slower and wasn’t able to keep up with the rate of change I was looking for. In the end, I used the analog signal and tried to filter it as best I could.

/*
Using the Analog signal from a Maxsonar LV EZ0 to drive a 
12V LED strip through a MOSFET. 
Take 20 samples, sort and select the median value.
*/

const int anPin = 9;
int anVolt, cm;
const int samples = 20;
int ranges[samples];
int highestReading;

float time;
float previousIntensity;
float maxDist;
float minDist;
int minVal, maxVal;

int mosfetPin = 9;           // the pin that the MOSFET is attached to

void setup()  { 
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
  Serial.begin(9600);
  time = 0;
  maxDist = 450;
  minDist = 10;
  minVal = 5;
  maxVal = 255;
  previousIntensity = 0;
} 

void loop(){
  // print time spent since last loop
  Serial.println(millis()-time);
  time = millis(); 
  
  // sample sensor value
  for(int i = 0; i &lt; samples ; i++)
  {
    anVolt = analogRead(anPin)/2;
    ranges[i] = anVolt;
    delayMicroseconds(50);
  }
  isort(ranges,samples);
  
  // convert to centimeters
  Serial.println(samples/2);
  cm = 2.54 * ranges[samples/2];

  // shape the curve of the result
  float intensity = (cm - minDist)/(maxDist - minDist);
  intensity = 1-constrain(intensity,0,1);
  intensity = intensity*intensity;
  intensity = previousIntensity*.995 + intensity*.005;
  previousIntensity = intensity;

  // set the brightness of pin 9:
  float val= intensity*(maxVal-minVal)+minVal;
  analogWrite(mosfetPin, val);
}

//Sorting function
void isort(int *a, int n){
// *a is an array pointer function
  for (int i = 1; i &lt; n; ++i)
  {
    int j = a[i];
    int k;
    for (k = i - 1; (k &gt;= 0) &amp;&amp; (j &lt; a[k]); k--)
    {
      a[k + 1] = a[k];
    }
    a[k + 1] = j;
  }
}

Next Steps

Once I get my paws on that Arduino Nano board, I can rework my circuit and get the soldering iron out to make my electronics more permanent. I have also ordered a HC-SR04 Distance Sensor to see how it compares to the Maxsonar in terms of accuracy, speed and noise. Also, I need to make the frame a little bit deeper so the light can spread out a bit further across the image.

Which one is cooler?

The ominous cyclopean look or the cute and friendly Wall-E look?