CSE 466 Lab 3: Pulse Width Modulation and Electric Field Sensing


In the first section of this lab, you will learn two methods for generating pulse width modulated signals with the ATmega16, which you will use to vary the intensity of the red, green, and blue LEDs in an RGB LED. You'll implement a simple colorspace conversion on your microcontroller to allow the hue of the LED to be changed with a potentiometer.

In the second section of this lab, you will learn how a general-purpose microcontroller can be used to implement a new type of sensor from scratch. You will build a one-dimensional electric field sensor that will be able to sense the distance to your hand or other grounded objects.

Suggested Reading and Resources

Helpful Hints

Part 1: Pulse-Width Modulation and Color Space Conversion

Completed E-Field Board
Figure 1. Relationship between hue angle and RGB values with saturation and value at 100% (source: Wikipedia)
Question 1. Imagine that you were using a processor that did not have three output compare pins available to generate the three PWM signals for the LED, but you do have a timer interrupt available. Write an interrupt service routine that implements pulse-width modulation for three LED channels in software. You may find it helpful to actually try implementing this exercise on your ATmega16.

Part 2: Building an Electric Field Sensor


In this part of the lab, and continued next week, you will build a simple electric field sensor. Your completed circuit will detect the distance between your hand (or another object with high capacitance to ground) and the board. You will send the reading from the sensor to the PC over USB, which will allow you to vary the hue, saturation, and value of a color by moving your hand over the board. This color will be sent back to your circuit, which will display it on the RGB LED.

Since making the electric field sensor work properly takes a bit of planning and calculation, this week you will do the planning and setup, and next week you will write most of the code for the implementation.

Theory of Operation

Our simple electric field sensor is simply a board with two electrodes in the form of large square copper regions. One of these electrodes will be the transmitter and the other will be the receiver.

We will use a timer and interrupt on the ATmega16 to generate a square wave at the transmit frequency. This square wave will go between 0V and 5V with a duty cycle of 50%. We will then feed this signal into a resonant LC circuit. If the square wave is at the resonant frequency of the resonator, then the output will be a high amplitude (on the order of tens of volts to a hundred volts) sine wave, which we will connect to the transmit electrode. The changing electric potential on the electrode will create an AC electric field.

The electric field emitted from the transmit electrode will pass through the receive electrode, which will cause a small AC current to be induced. We'll use two op-amps to modify this signal before feeding it into the ADC on our ATmega16. The first stage will convert the current flowing into the electrode to a voltage, and the second is a voltage-gain stage that will amplify the voltage to a range appropriate for the ADC.

We will then use the ADC to sample certain points on the received waveform, which will allow us to determine the phase and magnitude of the received signal.

When a grounded object, such as your hand, comes in range of the electric field sensor, some of the current from the transmitter is shunted to ground and less will go into the receive electrode. As a result, the magnitude of the received signal will be smaller.

Implementation Basics

There are two components required to implement the sensor on the ATmega16. To operate the transmitter and generate an alternating electric field, the microcontroller must generate a square wave at the desired transmit frequency. We'll do this with Timer 0's output compare interrupt. The associated ISR will be very simple: it just has to toggle a pin on and off. By selecting the prescaler and output compare values, we can precisely set the frequency of the square wave.

Question 2: Write an equation to determine the frequency of the transmitter output waveform fT for a given prescaler value pT0 and output compare value c. Remember that an output compare value of 63, for example, takes 64 timer counts to match (the timer starts at 0). The exact frequency of the crystal you are using is 7,372,800 Hz—do not use an approximation.

We will use the ADC in free-running mode to sample the received waveform. We want to take ADC samples at points corresponding to 0˚, 90˚, 180˚, and 270˚ on the waveform. In order to make this happen, the prescaler for the ADC and the prescaler and compare value for timer 0 need to be carefully chosen so that an ADC sample occurs some integer number of periods of the transmit waveform after the previous, plus one-quarter period (90˚).

Question 3: Write an equation to determine the sample rate of the ADC, fS, for a given ADC prescaler value pADC.

Now, we need to determine the frequency at which we're going to transmit, and the prescaler and output compare values that are necessary to set everything up. Ideally to be robust to noise we want to keep the transmit frequency as high as possible, within the constraints of our microcontroller. One important constraint is that the successive approximation ADC in our ATmega168 can't really provide useful values if its clock is running faster than 1 MHz. Another important consideration is the time it takes to handle the ISR for generating the square wave, which happens twice per period of the transmit waveform, and the time it takes to read and accumulate the value from the ADC, which happens once per ADC sample.

Question 4: Using your two equations and the above constraints, determine values for pADC, pT0, and c that satisfy the condition:

with n being a small integer. This relationship should be exact; an approximation won't work here!

Consult with a TA to verify that you have chosen reasonable values before proceeding.

Building the Sensor Hardware

Since the transmit circuitry is sensitive to capacitance, and the receive circuitry is somewhat sensitive to layout considerations, we've prepared a printed circuit board specifically for this lab. The PCB also contains the transmit and receive electrodes.

Refer to the schematic while assembling the board.

Completed E-Field Board
Figure 2. Completed E-Field Sensor Board
Question 5: Show your computation of the required capacitor value in your lab write-up.

Testing the Hardware

Setting Up and Checking the ADC Timing

If you have all of the above working, you should be in good shape for next week's lab, in which we will finish implementing the sensor and add USB communication to and from your PC.