Friday, October 5, 2018

Creating a DIP ATtiny85 Watch with the DS3231

-Joseph Bushagour

"Far out in the uncharted backwaters of the unfashionable end of the western spiral arm of the Galaxy lies a small unregarded yellow sun. Orbiting this at a distance of roughly ninety-two million miles is an utterly insignificant little blue green planet whose ape-descended life forms are so amazingly primitive that they still think digital watches are a pretty neat idea.” -Douglas Adams, The Hitchhiker's Guide to the Galaxy

As Douglas Adams explained in The Hitchhiker's Guide to the Galaxy, digital watches are "pretty neat" to us primitive life forms. Something about the marriage of practicality, and sheer nerdiness gets me oddly excited. Somewhere in my fascination I asked myself, "can I make a digital watch entirely of my design?" I did! And it taught me a lot about pcb fabrication, low power programming, and shift registers. 



((For a higher resolution video, check here))

DS3231, the Timekeeper

Probably the most important function of a watch is that it keeps time. While you could use your microcontroller to count the seconds and save on parts, there are some major downsides to this. For one, the microcontroller is much worse at keeping time than a dedicated RTC (Real Time Clock) IC, the time would drift significantly with temperature and battery voltage. Another serious problem is that it would require the microcontroller to always be on, keeping track of the time. This would consume much more current than an RTC IC, draining the battery significantly faster. Thus we employ a DS3231 to casually sit in the background, consuming microamps from it's own back-up battery (which, at the rate of 200µA, would take 12.56 years to drain).

ATtiny85, the Brains of the Operation

First released in 2005, the ATtiny85 is more recent than its larger cousin the ATmega328, (known from Arduino fame) which sits at an aged 1990s vintage. Still, the IC is easily found in the somewhat antiquated DIP package (dual in-line package) ((the term DIP package has a slight case of RAS syndrome)). Ignoring the pleonasm, the DIP package is extremely easy to fit on a breadboard, making it wonderful for prototyping, and a little big for production.


The ATtiny is programmed very simply for this project. The tactile switch that initiates the time display process is simply connected between the reset pin and ground. When it is pressed, the IC will restart and execute the following steps. First it uses pins PB2 and PB0 to communicate with the DS3231 through I2C and receives the time. Then it displays each digit of the time (with a decimal point denoting the ":" change from hours to minutes) on the seven segment display. Finally the ATtiny is put in its "deep sleep" mode indefinitely, only to be woken up by the reset pin whenever needed.

If you're particularly astute, you may notice that the ATtiny85 only has 5 usable digital pins (technically we could get 6, but it's a huge hassle to do so). Moreover, you might see that 2 of these pins are taken up by the I2C protocol. So how do we display a digit on a seven segment display when we only have 3 pins? Normal driving of the display would only allow us to use three of the segments, and we don't have access to all the anodes and cathodes, so charlieplexing is a no-go. The trick to driving this seven segment display with only 3 pins lays in 1970s technology; the 74HC595.

74HC595, the Driver


The 74HC595 is a remarkably old IC, dating back to the 1970s -- a time when solid state devices and ICs were still in the process of replacing good 'ol vacuum tubes. It exists in a series of logic ICs created in that boom known as the 74 logic series. The amount of different types of these logic ICs is immense, but all we're focusing on is the 74x595 -- a humble, but powerful SIPO (serial in, parallel out) shift register.

So what is a shift register? Well without diving deep into the specifics, a SIPO shift register can take 8 bits of serial input, and turn them into a parallel output on 8 of it's pins. Even more simply, you can give it 8 bits through 1 pin, and it will output those 8 bits individually on 8 pins (you could daisy chain SIPO shift registers as much as you want, turning 1 input into theoretically hundreds of outputs). Now it's not really as easy as 1 input to 8 outputs, in reality, you need 3 pins to control the shift register, 1 to give the serial input, and 2 more to control the clock and latch, but we have those pins to spare, so a shift register is perfect for us. Whenever a number needs to be displayed, we shift 8 bits into the shift register (corresponding to a number we want to display) and the 74HC595 drives the 8 inputs of the display.

The PCB



The watch wouldn't be all that useful if I built it on a breadboard and carried that around with me, so I figured I would design a PCB (printed circuit board) that I would then mount to a strap and wear around my wrist. I made the PCB in KiCad and got it fabricated by OSH Park (could have gone with dirtypcbs or allpcb, but I was impatient with the shipping).

I tried to pack all the components as closely as I could, but because I was using DIP packages, I couldn't really make it too compact.

What's Next?

The watch is totally functional, and the battery takes well over a month to discharge, but there are definitly improvements to be made. I might try to make a surface-mount version to decrease the size, and switch from the AtTiny85 to the AtTiny84  in order to omit the shift register. Currently the watch band is just attached on the bottom with some glue, I might 3d print a better case for the pcb with attachment points for the strap so it can be attached more ruggedly.


Wednesday, September 19, 2018

Driving an HC SR04 Ultrasonic Sensor and Controlling a Servo using only 555 timers.

- Joseph Bushagour

The objective was simple in concept: trigger a servo motor to rotate a specific amount when an object passes the ultrasonic sensor at a specific distance. An idea easily achieved with almost any microcontroller around (provided it has 3 usable pins and at least 1 timer). However, the easy way is no fun. So I set a challenge for myself, could I do it without any programmable components, or more specifically, using only the venerable 555 timer? The answer is yes! Although, I did need 4 555 timers, 3 transistors and a plethora of passive components.


The breadboard organization was modified for the camera

The 555 Timer

The 555 timer is ancient, first developed in 1972 by Signetics (Signetics would turn out to be a "one hit wonder" of sorts in the IC development industry, with no later release even getting close to fame that would follow the 555) and still being produced to this day. It acts as a sort of swiss-army knife for electrical engineers, finding use in timer, flip-flop, pulse generation, time delay, and oscillation applications.


Its working principle can be generally understood by looking at the function of each pin.

Vcc- This pin is connected to a voltage between 5V and 15V.

Ground- I'm sure you can figure out what this pin is connect to.

Trigger- If a voltage beneath 1/3 of Vcc is applied to the pin, the ouput pin will be set high and stay high until reset with the Threshold pin or Reset pin.

Output- The output pin can be set either high or low. When high, the voltage at the pin will be (Vcc - 1.7V). When low, the voltage will be almost 0V at the pin.

Reset- When pulled low, this pin will manually set the output pin to low.

Threshold- In some configurations of the timer, there will be a timing capacitor whose voltage is monitored by this pin. When the voltage on this pin exceeds 2/3 of Vcc, the output pin will be set low.

Discharge- This is the pin in which the timing capacitor is discharged through.

Control- Rarely used (when unused it should be connected with a 10nf capacitor to ground), this pin provides direct access to the internal voltage divider of the timer (as shown in the simplified diagram below).


Some details have been removed for sake of readability, most notably the output buffer and many inverting inputs
It's also useful to quickly go over two common configurations of the timer that are used in the project.

Astable mode- The output does not stay high or low, rather it oscillates in a square wave determined by some passive surrounding components. 

Monostable mode- In this mode, when a pulse of 0V is applied to the trigger pin, the output pin will be set high for a specific amount of time, then fall back low. 



The Circuit


The old-school hand-drawn way!


So how does the circuit work? How about we break it into what each 555 timer does.

555 Timer #1

This timer is configured in an astable mode, generating an approximately 40 kilohertz square wave that drives the trigger pin of our HC SR04 ultrasonic module. The module returns a pwm signal from the echo pin, the duty cycle increasing when the module is blocked (the closer the blockage, the higher the duty cycle). This pwm signal is then averaged into an analog voltage by a capacitor, which then feeds that into the second timer.


555 Timer #2

This timer is used as more of a hack than a normal use of the timer. It's not configured in an astable, monostable, or even bistable configuration. Instead, I'm using it as sort of "comparator", with it's trip point set by a voltage divider. When the HC SR04 is sufficiently blocked and the analog voltage fed into the second timer is high enough, the output pin of the timer with go high. This high pulse is immediately differentiated by the following RC circuit, turning the constantly high output into simple a voltage spike. This spike is then inverted from being positive (normally 0V, higher during spike) to negative (normally 5V, lower during spike) by a transistor inverter.  


555 Timer #3

This timer is configured in the monostable mode. When the quick negative spike is applied to the trigger pin, the timer's output will go high for much less than a second. This output is inverted to being low, which is used turn on the final 555 timer for an exact amount of time.


555 Timer #4

The negative pulse is connected to the ground pin of the timer, when the pulse is applied, the 4th timer is able to turn on. When this timer turns on, it is in an astable configuration, generating a square wave with a frequency of about 85 hertz. This is then used to drive the servo through a transistor (as the servo can draw much more current than the 555 is able to source). After the negative pulse on the ground pin is finished, the timer turns off. 



That's it!

While the circuit really isn't practical, as it would be ten times easier to use a microcontroller rather than the 555 timers, it makes up for that fact by being really interesting! Often I find designing systems with self-inflicted handicaps like this really help me view similar problems in a creative light and betters my electrical critical thinking skills. In fact, if you have a few 555s laying around, I'd recommend trying to make some challenging circuits with them yourself!

Here's a final picture of the result, hopefully now you can piece together what each part is doing.