Category Archives: Electronics

Calibrating the PCF8523 RTC without an oscilliscope

I recently purchased the excellent PCF8523 real time clock breakout board from Adafruit for a project. It was easy to interface with using I2C and the RTClib Arduino library. It keeps the time for up to five years even with no external power by using a coin cell battery.

The only trouble was that the clock was not terribly accurate and after a few weeks was fast by several minutes. A perfect crystal would oscillate at exactly 32.768 kHz but various factors like temperature, pressure and humidity make it faster or slower. In my case, the crystal was oscillating too quickly causing the RTC to be fast by 8 seconds per day.

After reading through the PCF8523 datasheet I found that the RTC can be calibrated by setting a value in one of the device registers. Section 8.8.3 describes a way to calculate the register value.

Offset calibration calculation workflow

I do not have an oscilloscope with a high enough resolution to accurately measure the frequency for step 1. Is it possible to calculate the offset without one? The NXP application note discussing improved timekeeping has some useful tidbits beyond what was described in the datasheet. Most important is the fact that 1 second of clock drift per day corresponds to an error of 11.57 parts per million for a 32.768 kHz oscillator. By measuring the drift of the clock over a few days we can do some simple calculations and calibrate the RTC.

RTC offset calculation

Record the current time along with how it compares to the clock on the RTC. Wait a few days and do it again.22:21 on Jul 18
RTC is behind actual time by 7 seconds

19:11 on Jul 21
RTC is ahead of actual time by 18 seconds
Calculate the time between your two measurements in days3 days - 3 hours, 10 minutes
= 3 days - 0.125 days - 0.00694 days
= 2.86806 days
Calculate the amount of drift between your two measurements7 s + 18 s = 25 s
Calculate the amount of drift per day25 s / 2.86806 days
= 8.7167 s/day
Using the fact that 1 second of drift per day = 11.57 ppm, calculate the drift in ppm.11.57 ppm/day * 8.7167 days = 100.85 ppm
Use the value from the datasheet to calculate the offset register value100.85 / 4.34 = 23.234 => 23 correction pulses

What does “correction pulse” mean? The RTC will extend or reduce the amount of time required to consider that a second has elapsed. This is the value that gets placed in the first (least significant) 7 bits of the offset register.

  • If your clock is running fast you need to write a positive value to the register. This increases the number of oscillations required for a second to elapse, thereby slowing down the clock.
  • If your clock is running slow you need to write a negative value to the register, coded in two’s complement. This reduces the number of oscillations required for a second to elapse, thereby speeding up the clock.

Writing to the offset register

Since the 8th bit of the register contains the mode flag, determining the final byte that should be written to the offset register requires some bit manipulation. I’ve added a calculate method to the RTClib Arduino library that does this for you. The calibrate function can be called like this:


ZXSC380 LED Driver Breakout Board

How to design and build a breakout board for an SMD component

As electronic components become ever more integrated and miniaturized, it can sometimes be impossible to find a through-hole counterpart to an interesting SMD part. For example, I wanted to do some prototyping with the ZXSC30 LED Driver but it’s available only in a SOT23 package. With some attention to detail and the right equipment, it’s not difficult to design and build your own breakout board for such a component.


1. Design the Circuit

If you scan through the datasheet you’ll often find that this step is done for you, or nearly so. Look for a diagram called something like “Typical application circuit.” It is nice to include required passive components on the breakout board to make it as “plug and play” as possible. In this case, the only external component required beside the LED itself is a small inductor.

Typical application circuit for the ZXSC380

Load up a PCB design app like Eagle and re-create the circuit there.

Eagle schematic

The Eagle design files for this breakout board are available on GitHub.

2. Print the Circuit Boards

Oshpark is a circuit board printing service that is perfect for hobbyists. Create an account, upload your Eagle files, complete the purchase and you can expect to have perfect purple PCBs in your hand in a couple weeks. I’ve shared my board design in case you’d like to use it.

Board preview from Oshpark

and IRL

3. Create a Solder Paste Stencil

OSH Stencils is a great service to create a solder paste stencil. Again, create an account and upload your Eagle file. OSH Stencils will use the solder mask layer of your board design to fabricate a polyimide or stainless steel stencil perfectly matching your PCB.

Stencil preview from OSH Stencils

and IRL

3. Assemble the Board

Scoop up some solder paste using the squeegee provided by OSH Stencils and spread it over the aligned stencil. Then scrape over the board using an edge to force paste through the stencil. Scrape the extra solder paste back into its jar and carefully remove the stencil. If the solder isn’t lined up the way you hoped, clean it off with an alcohol wipe and try again.

Applying the solder paste

Use fine point tweezers to place the components. The pads should be somewhat aligned but do not need to be perfect. This is because once the solder liquefies, the surface tension tends to pull the component into place.

Turn on the hot air rework station and allow it to heat up. Then move the nozzle over the PCB until the solder liquefies. Probably don’t do this on your wood table top =P

4. Test

Create a simple test circuit for your PCB to make sure it works. In this case I connect a AA battery to VCC/GND and an LED to VOUT/GND. A single AA battery does not have enough voltage to power an LED on its own but with the ZXSC380, the LED lights up brightly. Neato!

micro:bit Compass

The other day I bought a micro:bit for my daughter. It’s a small computer with bluetooth, an accelerometer, light sensor, magnetometer and temperature sensor. It also has a couple of buttons for input and a matrix of LEDs for output. It’s become obvious that I’m way more interested in playing with it than my daughter is but I’m hopeful that after I show her what it can do, she’ll take an interest in writing some code for it herself.

My first small program takes the input from the compass and writes out the cardinal directions, N, S, E and W.

This is what the program looks like in Blocks

and here is the equivalent JavaScript code

Below is an emulator running this code. Drag the compass near the top and you will see the text on the micro:bit change.

Finally, here is a video of the code running on the actual device

If you’d like to play with this project yourself, you can do so here:

Fractal Design R4 Headphone Jack Repair (USB-70A AZALIA)

I’ve been using the Fractal Design R4 PC case for a number of years now. I love the design but the placement of the headphone jack leaves something to be desired. It is oriented vertically meaning that pulling on the headphone cable can easily break the port which unfortunately happened to me. Removing the front panel of the case reveals a small circuit board labeled “USB-70A AZALIA REV: A1”.

The circuit board with damaged headphone jack

Not designed with toddlers in mind

A quick search showed me that it’s not easy to find a replacement for this part so I decided to write up how it can be repaired.

The board consists of two headphone jacks and a pin header. The broken headphone jack needed to be replaced. The first step is finding the correct replacement part. Some searching reveals the CP1-3525NG-ND on Digi-Key. I always order inexpensive parts in multiples of 10 to get the price break and because it’s good to have spare parts around for future projects.

A handsome array of headphone jacks

Using a soldering iron, heat up and remove as much of the solder around the pins of the headphone jack as possible. I didn’t have much luck with soldering wick but a soldering vacuum worked well. A trick is to add a small amount of solder to the tip of the soldering iron to help quickly heat the solder in the joint.

After removing the solder, gently remove the old headphone jack.

PCB after removing the broken headphone jack

Solder the replacement headphone jack into place. Heat the pin using the iron and apply enough solder so that it flows into each via.

I ended up replacing both jacks because the replacements have a metal ring that looks more sturdy than the original.

PCB with replacement headphone jacks

Both my headphones and a microphone worked great after putting everything back together. I was initially worried that the pinout of the replacement jacks might be different than the originals but they turned out to be an exact match.