Barker Code-Locked Loop Synchronous Demodulator using ATtiny2313 microcontroller

A simple, low component count phase locked loop that locks onto and detects the amplitude of an incoming baseband 7 bit Barker code using a switched resistor demodulator that is driven directly by a microcontroller’s output pins.

  • Balanced modulators using resistors and a microcontroller’s output pins.
  • Locking into oncoming Pseudo-noise (PN) stream.
  • Microcontroller modulates its own crystal oscillator frequenc inside a Phase-Locked Loop (PLL).
  • Extending the output range of a bipolar op-amp to opearte near ground.

xmission lesssm
Photo 1. The only semiconductors in the circuit are the microcontroller
and a quad opamp. This picture was taken before the offset adjustment
for the signal level measurement circuit was installed. Some surface
mount components are on the other side of the board.
Click on the image above for a larger version.


Download the AVR Studio assembly source for the ATtiny12 7 bit Barker beacon: Brkshf061015B.asm
The AVR Studio assembly source for the ATtiny2313 demodulator / 7 bit Barker source: bark3p061015A.asm
Download the AVR Studio assembly source for the ATtiny2313 181.818 kHz Barker code transmitter: rf7bark061015B.asm
Find updates at
Related web pages:
Digital Lock-in Milliohmmeter
Experimental 1 kHz Synchronous Demodulator
This simple circuit includes three synchronous demodulators and two
integrators that process the demodulation signal. The modulation is achieved by switching
the microncroller’s I/O pins between high impedance and current sinking states, so no additional
analog switches or multipliers are needed. The first op-amp stage (pins 8,9, and 10) is used
to provide a uniformly low output impedance to the demodulators. It can be converted to a gain
stage or a filter, provided there is sufficient loop gain to maintain a sufficiently low output impedance.


After a successful synchronous demodulator project, as described in Digital Lock-in Milliohmmeter and Experimental 1 kHz Synchronous Demodulator, I looked for a way to improve upon on the basic synchronous demodulator that I used in those projects. I was looking for a way to reduce the component count, eliminating the transmission gates if possible. I also wanted the demodulator to be able to lock to signal from a remote source, all the while maintaining the benefits of noise rejection and processing gain that one would expect from a synchronous demodulator,  because such a circuit might lead to a simple but robust modulation and demodulation scheme for RF communications.
The circuit in figure 1 is that of a synchronous demodulator that meets these requirements, and additionally, it operates from a single positive power supply. The circuit will be described in detail later in this page. The ATtiny2313 microcontroller is  used as a voltage controlled crystal oscillator and Barker code generator, and could be replaced with small scale logic devices. The code generation is initiated by interrupts from the microcontroller’s internal 8 bit timer, and there is plenty of time available for control and computational tasks that could used to modulate the Barker code stream with data or to decode an incoming stream.
The ATtiny2313 has three Barker code outputs, shifted one half of a chip in period (the time period allotted to one bit in the Barker code) from one-aonther, that appear to the external circuitry as though they are open drain outputs,  it has three regular CMOS outputs that are similarly time shifted with respect to one another, and it has a sync output that occurs one time with each repetition of the Barker code. The sync signal is to facilitate testing of the circuit.
This implementation, using a 7 bit Barker code with a 148 microsecond chip rate, was intended as a means of proving a concept. The low chip rate was selected because it is easy to deal with using the inexpensive equipment available to me.  A longer Barker code and a faster chip rate would increase performance, and merely scaling the design will provide those improvements.
The circuit of an infrared beacon, which transmits a continuous
stream of the 7 bit Barker code. The Barker code gates a 50 milliamp
constant current source made of the two 2N4401 transistors.

As a test signal source, I programmed an ATtiny12 microcontroller with code similar to the ATtiny2313 and uses its code output pin to drive a gated constant current source, which in turn drives an infrared LED with 50 milliamps. The LED emitted light in accordance with the Barker code sequence sent from the chip.
As with the ATtiny2313, this function could be easily replaced with discrete logic. In addition to the code output, the ATtiny12 also has a sync output that occurs one time with each repetition of the Barker code.In an experiment to visually illustrate operation of the demodulator, I connected a photo diode between the signal input and “ground” on the demodulator circuit in Figure 1 and used the 7 bit Barker code beacon shown in Figure 2. There was no electrical connection between the beacon and the demodulator, except that their power supplies were plugged into the same electrical outlet. During some experiments, the beacon was battery operated, and I did not notice any difference in performance between those experiments in which the beacon was battery operated and those experiments in which the beacon was powered from a power supply.
Before testing, the free run frequency of the demodulator had to be set. Channel one of a Tektronix TDS-2002 oscilloscope was connected to the  punctual Barker code output pin on the ATtiny2313 (ATtiny2313 pin 18), and scoope was sync’d to the sync output of the ATtiny2313 (ATtiny2313 pin 16). Channel two of the scope was connected to the Barker output of the ATtiny12 on the beacon assembly (ATtiny12 pin 7) and the infrared LED on the beacon was covered so demodulator would not detect the beacon’s signal. While simultaneously viewing the prompt Barker code generated on the demodulator and the Barker code generated by the beacon, the free run frequency of the demodulator was adjusted so that rate of local prompt Barker code generation was fraction of a word per second slower than that of the beacon.
Channel two of a Tektronix TDS-2002 oscilloscope was then moved to the  output of the voltage follower stage in the demodulator (LM324 pin 8) and the IR LED on the beacon was uncovered. letting some of the light from the LED fall on the photo diode connected to the demodulator. Photo 1 and Photo 2 show what the signals looked like.

Photo 2. This is a screen shot while the scope (Tektronix TDS-2002)is in the sampling mode. The blue trace shows the incoming signal from a photo diode, with the noise provided by fluorescent lights and a dangling 1 meter clip lead. The yellow trace is the prompt Barker code generated by the ATtiny2313 on the demodulator. The scope was synchronized to the sync output on the ATtiny2313.

Even though the singal-to-noise ratio was rather poor. The demodulator managed to lock to the incoming signal and so provided sync for the scope. So when I switched the scope to Averaging mode, in which the waveform was averaged over 128 cycles, the received waveform was displayed clearly.
The averaging of the waveform samples by the oscilloscope is analogous to the function of the demodulator. The average value of random noise is zero. Nearly every signal that is not the expected Barker code appeared as noise to the integrators following the demodulator stages, and since the output of the integrator is the average of input signals over a period of time, the noise was averaged to a very low level.


To appreciate the basics of lock-in amplifiers, please read the article,  Digital Lock-in Milliohmmeter. The article explains and illustrates the concepts in some detail. The Lock-in Milliohmmeter described in the article uses a 1 kHz square wave which is generated by the microcontroller, converted to a current waveform, and then measured through the process of synchronous demodulation. Another article about the same synchronous demodulator,
Experimental 1 kHz Synchronous Demodulator, examines the sensitivity of the demodulator and its ability to measure very small signals in spite of the circuit relying on a quad op-amp that is not known for its low noise performance or exceptional DC stability.
The main problem with using a simple square wave for low level measurements is that they are susceptible to noise, especially at the signal frequency and its odd harmonics. One solution to this susceptibility is to transmit and demodulate with a signal that is made of many different frequencies, one that effectively looks like random noise, but isn’t really random. This signal is referred to as pseudo noise, or PN, in many signal processing articles. Figure 3 and Figure 4 show the 7 bit pseudo random sequence used in this series of experiments. This 7 bit sequence is the 7 bit Barker code. All versions of a particular Barker codes that are inverted and/or shifted in time are the same Barker code.

About The Author

Scroll to Top