Monday, May 16, 2016

Designing a Mains Frequency Monitor, Part 1

This is the first in a series of posts about designing a mains frequency monitor using the Microchip PIC 16F1619 microcontroller. Today we will talk about the features of this device that make it a good choice for such a project. We will also start to piece together our circuit, program, and test a prototype on the breadboard.

Prototype of a mains frequency monitor using the PIC 16F1619.

Introduction


Many people assume that the mains frequency is exactly 50/60Hz, but in fact that is rarely true. Accuracy and stability of the mains frequency is an interesting topic to research. No two points on the grid will have exactly the same characteristics, so collecting data at your location can reveal some interesting findings. This is especially true if you have (or are interested in building) a mains-referenced clock. There are many industrial applications as well for such a project.

Numerous circuits and methods have been published for monitoring the mains frequency. Recently, I came across the PIC 16F1619 microcontroller and thought it would be a great device to design my own solution around. It is a very inexpensive chip that comes in a DIP package, making it breadboard-friendly for prototyping. This PIC has an interesting set of features including a Zero Crossing Detector (ZCD), a Signal Measurement Timer (SMT), and an Angular Timer. It also includes several 16-bit timers, configurable logic cells, a PID math accelerator, a hardware USART, and lots more. Check out the datasheet for this device to learn more.

Pinout of the 16F1619 in a 20-pin PDIP/SOIC/SSOP package.

Zero Crossing Detector


The first peripheral we will examine in more detail is the ZCD. It allows the detection of both the positive- and negative-going zero crossings of the input signal. Additionally, the output of the ZCD can be used as an input to another peripheral such as the SMT for gating and windowing. Externally, it requires only a single series resistor to limit the current into the module. Additional pull-up and pull-down resistors can be added to correct the offset on the comparator reference voltage or intentionally set an offset from true 0V. Check out this Technical Brief to learn more about the capabilities and setup of the module.

Block diagram of the ZCD module.

Scope capture of an AC input signal (blue) and ZCD output signal (yellow).
The output is also accessible in an internal register for triggering other peripherals.

Signal Measurement Timer


The SMT is a flexible 24-bit timer with many options for clocking, windowing, and gating. Check out the Technical Brief to learn more about it. We can use it to measure the relatively long cycles of the mains signal at full system clock speed and still not overrun the timer. Combined with the ZCD it gives us a means for measuring the mains frequency to a suitable level of precision. With the core functionality needed for our project handled by integrated peripherals, we save our code space for processing the data and feeding it out to another device for monitoring and logging.

Block diagram of the SMT module.

Angular Timer


Another really cool peripheral in the 16F1619 is the Angular Timer. It locks onto the input signal and allows the instantaneous phase angle to be determined at any point during the cycle. It can also be used for missed pulse detection. This peripheral relies on several registers and configuration bits, but once set up it is easy to use. You simply read out the value of the phase clock during a cycle to calculate the angle. This peripheral has many potential uses for mains applications and motor control. Microchip has a webpage on it with more information, documentation, and a video.

Designing the Circuit and Firmware


With the above three peripherals in mind, I started working on my circuit design for the project and building a prototype on the breadboard. I added a 20MHz TCXO to generate a stable, accurate clock for the microcontroller and timers. For the mains input, I used a 12V AC wall adapter. This goes into a half-wave rectifier and then a voltage regulator to generate VDD. The mains signal to be measured is tapped off before the rectifier, bringing it into the ZCD with a common ground reference. I might explore upgrading to a center-tapped full-wave rectifier in the future.

Below is a partial schematic for the circuit thus far.

The beginnings of a mains frequency monitor circuit.

The firmware came together quickly in MPLAB X IDE. I set up the various peripherals and put the SMT into Windowed Measure mode. The window is triggered by the internal ZCD output bit. When a positive-going zero crossing is detected, the SMT beginning counting at the full 20MHz system clock speed. At the end of the cycle the count is latched into a register and then stored. Once measurements for 60 cycles are accumulated, the average frequency during the second is calculated and printed via USART to an LCD display.

I didn't actually need the Angular Timer in my project, but it's such a cool peripheral I wanted to find some way to include it. I decided to use it to verify that the negative-going zero crossing occurs at the correct phase angle. That angle is of course 180 degrees (assuming a symmetric waveform). The ZCD generates an interrupt at the zero crossing and the program checks the count in the phase timer. If the phase angle is not correct, an error flag is set. Currently I just use this to set a status LED, but there is a lot more that could be done with it. In the future I might use it to detect erroneous measurements and automatically remove them before they get ingested into my data.

This hardware and firmware setup also gives us the basis for a mains-referenced clock, where the time is incremented every 60 cycles. That doesn't actually provide very good timekeeping (over short intervals) due to the instability/innacuracy of the mains frequency. For now it's an interesting addition to the project though. We will revisit this later and implement a much better solution for our clock.

The prototype on the breadboard works well, and you can see a picture of it at the top of this article. My initial testing against proper frequency counters shows that the design so far meets my expectations.

Lots More To Do


The project is coming along well, but we have a lot more to do before it is done. In Part 2, we will finish the firmware, finalize the schematic, and design a PCB for it. We will also need to do a lot of testing to verify the performance of the counter.

I have uploaded the program in its current state to a new Github repo. Please check it out if you are interested in seeing what the code looks like. This repo will also house the PCB design files and other documentation in the future.

If you have feedback and suggestions on this project, please post them in the comments below. A post on Part 2 of the project will be coming in a couple of weeks.

Thanks for reading!

- Dan W.

Saturday, May 14, 2016

Quick PCB: TCXO Breakout

I'm back with another Quick PCB. This one is a little breadboard-compatible breakout board for inexpensive temperature-compensated crystal oscillator (TCXO) modules commonly found on eBay.

Need a TCXO frequency reference for your breadboard project?
This might be a good option for you.

Introduction


There are some good, inexpensive TCXO modules on eBay that come in the slightly unusual package shown above. This breakout board allows you to mount one of the modules onto the small PCB and adapt it to standard 0.1" breadboard-compatible headers. I have also included a few optional SMD components. There are spots for a power LED, a bypass capacitor on the supply rail, and a capacitor on the control voltage input.

Some of these modules don't actually have a control voltage input on pin #1. Typically they have a small trimmer on top for adjusting the frequency, which is probably more useful for how these are commonly used. If you have access to a disciplined frequency reference such as a GPSDO, you should allow the module to warm up for 24 hours and then trim the frequency.

I have tested this board with two such TCXO modules from eBay. One is a 20MHz Raltron unit that is quite useful for microcontroller projects. You could also use a D flip-flop to divide it to 10MHz. The other is a 14.4MHz Rakon unit. Other modules in the same package will work. I honestly don't know what the package is called, so if YOU do please let me know in the comments below.

Assemble Your Own


Eagle Files: Shared on Github
TCXO Breakout Board: Order PCBs on OSHPark!

TCXO Breakout Board Schematic

TCXO Breakout Board Layout
Sharp-eyed readers will note this has been updated from the boards shown above.
The circuit is the same but the layout is much cleaner.

Parts List:


TCXO Module: Raltron RTX0230LC, Rakon VTX0525-14.4MHz, or similar
C1: 100nF 0603 SMD
C2: 10nF 0603 SMD (Only necessary if your module has VFC on pin #1)
R1: 1k 0603 SMD (or adjust value to change brightness of LED)
PWR LED: 0805 SMD (color of your choice)
Headers: Four 2x1 0.1" standard male headers (snap off from breakaway strips)

Note: The outer-most pins on each header are not connected to anything. They are just there for stability on the breadboard.

Sine to Square Wave?


Many of these modules output a relatively weak sine wave. However, you probably need a logic-level square wave for your project. You can use the circuit shown below to convert the sine wave output to a square wave. Note that you may need to adjust the resistor divider values based on the output signal of your module and the inverter you use. The peak-to-peak output from the TCXO needs to extend past the High and Low logic thresholds of the inverter to successfully trigger it. The divider also adjusts the duty cycle of the output.

Use a schmitt inverter chip with TTL-compatible inputs. I use 74LVC chips exclusively for my projects. 74ACT is another good series. If you want to be really old school, find those 74Fs in the junk box.

A typical circuit for converting a sine wave signal to a logic-level square wave. 

Wrap Up


I hope this little breakout board is useful to you! Tweet me a pic @sync_channel if you build one into a project.

Thanks for reading!

- Dan W.

Monday, May 2, 2016

Learning Embedded Electronics with the ATtiny104 XNANO

In this post I will document a small add-on board that I designed for the Atmel ATtiny104 Xplained NANO. I have also created a series of lessons that use the ATtiny104 XNANO with the add-on board to provide a basic introduction to embedded electronics and programming.

The ATTiny104 XNANO next to the small add-on board I designed to
enable a series of lessons on embedded development.


Introduction


In a previous post I examined the new ATtiny104 microcontroller from Atmel and checked out their evaluation board for it, the ATtiny104 Xplained NANO. I was very impressed by this little board. It has a programmer/debugger built in, allowing it to integrate seamlessly with Atmel Studio 7. You simply connect it to your computer with a USB cable to power the board, develop programs and connect to the USART. It is a great bargain at less than $5 USD.

It is not only suitable for evaluating the ATtiny104 but also for learning and teaching embedded electronics and programming. Certainly there are numerous Arduino and Arduino-like boards available for that task as well. This is just one more option that might be interesting if you need a very low-cost board that integrates with Atmel Studio for programming in standard C.

I designed a small add-on board for the ATtiny104 XNANO that includes a 5mm LED, a trimpot, and a photoresistor. The LED goes to one of the PWM-capable pins on the microcontroller, and the outputs of the trimpot and photoresistor circuit can be read using the ADC. These components combined with the on-board push button and LED allow for many lessons and projects to be completed. It's a very small and low-cost combo for learning or teaching embedded development.

The add-on board connected to the ATtiny104 Xplained Nano.

Lessons


To support the use of the add-on board, I have created a 28-page guide with eight lessons on basic embedded programming. The lessons walk you through using various features of the microcontroller and add-on board, including: digital inputs and outputs, the ADC, PWM output, USART, and interrupts. They are designed for someone that knows a little about electronics and programming, probably with an Arduino, and wants to get a feel for programming in Atmel Studio in standard C.

The guide to support the add-on board includes eight lessons on basic embedded development.


Add-On Board Kits


The add-on board makes for a very nice first soldering kit. The PCB is cheap ($6 USD for three copies from OSHPark) and the parts are very common, through-hole components. You probably already have all of this stuff on hand. The part outlines and resistor values are shown in the silk screen of the board so you don't even need to consult a schematic while assembling it.

ATtiny104 Xplained NANO boards can be acquired from distributors such as Digikey and Mouser. The PCBs can be ordered from OSHPark. Finally, the parts can be ordered from an online vendor such as Sparkfun

Lesson #1 in the guide is soldering together your add-on board kit!


Motivation


Let me say now that this is not a Kickstarter or anything like that. I'm not planning to sell these kits or make any money off of the guide. The add-on board is Open Source Hardware, and the guide is released under the Creative Commons Attribution-Share Alike 3.0 license. I hope that the board and guide are useful for learning a little bit about programming microcontrollers and embedded design, whether that is for an individual reading this blog or someone that might use the board for a workshop or class. Feel free to modify and redistribute the guide to suit your needs, as long as you attribute and share it back to the community under the same license. I have also shared the Eagle files for the add-on board if you'd like to customize it.

Check out the guide to learn more about the lessons and what you can do with this add-on board.


Shared as Open Source Hardware (OSHW)


ATtiny104 Xplained NANO Add-On Board: Order PCBs on OSHpark!

Eagle Files and Lesson Guide: Shared on Github

Schematic for the ATtiny104 XNANO Add-On Board

Parts list:
  • 10kΩ trimpot with knob
  • Small photoresistor (5mm width)
  • 5mm LED (color of your choice)
  • 470Ω resistor, 1/4 watt, 5%
  • 10kΩ resistor, 1/4 watt, 5%
  • (2) 12-pin 0.1” female headers (snap off from a strip of breakaway headers)
  • (2) 12-pin 0.1" male headers for the XNANO board

Note: There are no component IDs in the silkscreen, but the outlines and resistor values are shown to clearly indicate where the parts go.

Headers on XNANO: I have noticed that one of the capacitors on the ATtiny104 XNANO board is a little too close to the header pins. Male headers might not seat all the way flush against the board. Luckily there is an easy fix. Use a file to slightly round the bottom corner of the header where it hits the capacitor. Just a few swipes with the file will allow it to seat fully. Also, I used male headers on the XNANO board and female headers for the add-on board, but you can reverse that if you like.


After you finish the lessons and remove the add-on board, many additional
projects can be completed with the ATtiny104 XNANO.


Wrap Up


I hope that you find this small add-on board for the ATtiny104 XNANO and the lesson guide useful. If you assemble one and work through the lessons, or use them in a class or workshop, please let me know. Also, please post any questions and comments down below.

Thanks for reading!

- Dan W.


Saturday, April 30, 2016

10MHz OCXO Teardown (NDK ENE3311A)

Here we go with another OCXO teardown. This time we are taking apart an NDK ENE3311A 10MHz OCXO. This is a little 1" square unit with a square wave output. You can find them on eBay for about $15 USD.

I have a bunch of these on hand, so I don't mind sacrificing one
to learn more about the internal construction.

I cut around the bottom of the can with a dremel.
It eventually came off with little drama.

Not a lot going on inside this unit, eh? Just like in the CTS OCXO,
the oscillator circuit is on the top of the PCB, and the crystal is
bonded to the bottom side.

Close-up view of the top of the PCB, along with annotations of the pinout.

I cut the pins and flipped the board over. Ok! This side is much more interesting.
The little IC in the upper right is the output buffer. One of the SOT-23s on the
left is a voltage regulator. The remaining IC is probably an opamp.

There's a thermal pad and some white goop bonding the crystal to the heater elements.

I scraped away most of the thermal compound and lifted up the crystal.
We have two power devices and two resistors acting as heaters, just as expected.


That's all for this teardown! I might reverse-engineer the schematic for this one as I did with the CTS 1960017 OCXO. However, it's pretty easy to see what is going on inside this unit. Besides getting exact part values and device IDs, there is probably not a lot to be gained. What are your thoughts on the internal construction of the NDK ENE3311A OCXO?

Thanks for reading!

- Dan W.





Saturday, April 16, 2016

Frequency Counter OCXO Upgrade on The Signal Path

Shahriar of The Signal Path Blog posted a YouTube video a few months ago about a Fluke PM6680B frequency counter that he has in his lab. He performed a successful repair on the counter in that episode and got it fully operational. The unit was equipped with the stock XO timebase, however, that did not provide the necessary stability for a counter of that resolution.

I sent him one of my PM66xx OCXO upgrade boards for his unit. Check out his video where he installs and tests the board in his counter.


This upgrade board is shared as open source hardware. If you have a compatible counter and would like to make your own, check out my write-up here.

- Dan W.

Wednesday, April 13, 2016

Fluke/Philips PM66xx Frequency Counter OCXO Upgrade

In this post I will document an OCXO upgrade board I designed for the Fluke/Philips PM66xx line of frequency counters.

Custom OCXO upgrade installed in my Philips PM6674.

Introduction


A few months ago I purchased a Philips PM6674 frequency counter on eBay. It's an older 9 digit counter with two channels that has a maximum input frequency of 550MHz. The design feels dated compared to more modern counters, such as my Agilent 53131A. However, it is still a fully functional piece of lab equipment with a simple user interface and compact design. I often prefer older counters for day-to-day use because I don't have to fuss with complicated menu-based interfaces and features that I don't need. (Set the gate time on a 53131A and count how many button presses it takes).

A nice old frequency counter: The Philips PM6674.

My counter came with the standard XO timebase option, which has fairly poor specs for stability and drift. It is difficult to trim precisely with the single-turn trimmer capacitor on the board. For most testing in my lab I use an external reference from a GPSDO, but it is still nice to have an accurate timebase available in the counter if I need to take it somewhere and do testing away from the bench.

Previously I posted about an OCXO upgrade I made for my Racal-Dana 1992. The fun of designing a similar upgrade for the Philips counter was one of my motivations for purchasing it. My upgrade board is roughly equivalent to the original PM9691 OCXO module, and it should be compatible with any Fluke/Philips counter that is capable of using that option.


Designing an OCXO Upgrade


Creating a timebase upgrade board for the PM6674 was a bit more involved than the process for designing the Racal-Dana upgrade. That counter had a clean 5V supply available on the header where the timebase board connected, which was exactly what I needed for the OCXO. The header for the OCXO module in the Philips counter also has a 5V rail. However, that rail does not stay active when the counter is in standby. The original Philips OCXO modules operated from the 24V rail, and I had to use that for my own upgrade. In my counter, the "24V" rail actually runs at about 27V, and drifts up to 30V when the counter is in standby. There is also a substantial amount of ripple.

I opted for a Recom 78C5.0 DC-DC converter to get the 5V I needed to power my board. This is a nice little module with a pinout that mimics the 7805 linear regulator. It has good specs for efficiency (as high as 96%) and was very easy to implement on my board. I used a combination of electrolytic and ceramic capacitors on the input and output for filtering.

With power taken care of, the remainder of the board is very simple. I used the CTS 10MHz OCXO that I did a teardown on and reverse-engineered the schematic for. This OCXO has a 4V reference available on one of the pins, so I did not need to add a separate reference IC to the design. Additionally, the CTS OCXO has a ~1.4Vpp sine wave output, which is quite compatible with the Philips counters. The input spec for the internal reference signal is 1Vpp into 1kohm.

My take on a modern replacement for the PM969x series of OCXO modules.


The form-factor of my upgrade board mimics the original OCXO module as closely as possible. I found the exact header I needed on Digikey for the mounting arrangement used in the Philips counter, and there is a single hole for a securing screw. The module mounts nicely in my counter and looks pretty snazzy. The performance of the CTS OCXO is sufficient for the 9-digit resolution of the counter. It warms up quickly and hugely improves the accuracy and usefulness of the counter when it is away from a proper 10MHz external reference.

The CTS OCXO warms up very quickly.


Shared as Open Source Hardware (OSHW)


Fluke/Philips PM66xx OCXO Upgrade: Order PCBs on OSHPark!

Eagle Files: Shared on Github


Schematic of the PM66xx OCXO upgrade board.


Parts List

OCXO: CTS model 1960017 (available on eBay) or compatible substitute
DC-DC Converter: Recom 78C5.0-1.0
C1: 100uF / 50V, 105*C rated electrolytic (8mm diameter)
C2: 2.2uF X7R 0805 50V rated ceramic
C3: 220uF / 35V, 105*C rated electrolytic (8mm diameter)
C4: 10uF X5R 0805 25V rated ceramic
C5, C6: 100nF 0603 ceramic
R1: 51 ohm 1206
VR1: Vishay M64Y103KB40 10k 21 turn pot
Header: Molex 22-14-2104

The ceramic capacitors install on the bottom side of the board.


Installation Note: On some counter models, such as the PM6674, you need to remove the stock 10MHz crystal when you install this board. I simply cut the two legs of the crystal from the top of the board and left it glued in place, as you can see in the first picture in this article. Other counter models such as the PM6680B have jumpers near the crystal to easily disconnect it and select the OCXO reference.

12V/24V Rail: Some frequency counters in the series, such as the Fluke PM6680B, have a 12V rail available for the OCXO instead of 24V. This upgrade board is still compatible with those models, as 12V is sufficient to operate the DC-DC converter.

Rev A?


Sharp-eyed readers will note that this shared version of the board is Revision B. Well, that begs the question: what happened to Revision A? The original version of the board used a different OCXO, the 1" square NDK unit that I incorporated into my Racal-Dana upgrade. Due to the lack of a voltage reference on that OCXO, I had to incorporate an LM4140 into the design. The square wave output of that unit was also not ideal for the Philips counter. The whole design seemed needlessly complicated to me, especially when I knew I could make a more elegant solution based around the CTS OCXO.

Revision A of the PM66xx OCXO upgrade board. This one didn't make the cut.


Wrap Up


Please let me know if you assemble one of these boards for your counter! I have also provided the Eagle files, so you can modify the board if you like to use a different OCXO footprint. I have an entire case of the CTS OCXOs to use up, so expect some more shared projects that use them in the future.

Thanks for reading!

- Dan W.

Monday, April 4, 2016

New "Lab" Equipment: Nikon D3300 DSLR

I have needed a good camera for some time now. I've always been interested in photography and owned a couple of film SLRs back in the day, but never made the jump to a DSLR. The cameras in modern smartphones are so good, it's really easy to get by without a proper camera. As of today, however, I have some new equipment in hand to re-kindle an old hobby.

My new Nikon D3300 DSLR.
Photo taken by an iPhone 6S (and heavily post-processed).

Nearly all of the photos on this blog were taken by my iPhone. It has a decent camera, but taking pictures of small objects in indoor lighting can be challenging even with a good set of equipment. Two issues I constantly struggle with are underexposure and white balance. I have to heavily post-process the photos to make them presentable, and even then I can get some very strange color and noise artifacts. Lately I've just been blowing out the white background by cranking up the exposure, and then tweaking the temperature until it it looks "ok".

That all changes today! I am now the proud owner of a Nikon D3300. It is certainly an entry-level DSLR, but it has the features I need at a good price. I'm really looking forward to using it over the next few weeks and re-learning all the things I have forgotten about proper photography. The camera is also capable of recording video at 1080p/60fps. One of my plans for the blog is to launch a YouTube channel and start producing video content. This will be a starting point equipment-wise for getting that up and running.

I can't offer a lot of insight on the camera right now, but I did set up the tripod and take a couple of sample photos.

My Citizen Eco-Drive. Watches are very photogenic, aren't they?

A lovely open-source frequency counter board by Andy Brown.
I need to do a post about this one in the future.

What do you think of the above photos? Both would have been quite difficult to do with the iPhone. I certainly notice huge differences in the raw images but they may not come through in the uploads. I did have to do some post-processing, primarily because of the poor lighting I have available to me. I am planning to purchase a soft box to help with the lighting situation for taking pictures of my projects.

That's all for now. Expect higher quality photos (and videos!) on the blog in the future.

Thanks for reading!

- Dan W.