Measuring Acceleration Using a PIC Microcontroller


Keywords: 16f84, pic tutorial, pic microcontroller, g force, acceleration machine

Motivation and Audience:Motivated by one of the article from circuit cellar, I want to trace the author's steps to see how the G-Force machine work. The author wrote an article on how to build a machine, that measure acceleration using a PIC 16F84 and a acceleration sensor.

Audience will require expience in programming PIC Microcontroller (intro to microcontroller) and basic electrical background

Part List

US-based vendors include Jameco, Digikey, JDR and Radio Shack.

TABLE 1: PIC16F84 G-Machine

PART DESCRIPTION

VENDOR

PART

PRICE (USD)(2003)

QTY

MICROPROCESSOR

 

 

 

PIC16F84-04/P

JAMECO

145111

5.95

2

SENSOR

 

ADXL202E

www.analog.com

ADXL202

11.64

2

CAPACITORS

 

 

 

 

0.1 UF CAP

JAMECO

151116

1.00 FOR BAG OF 10

Or 10 Caps for Each type

22 pF CAP

JAMECO

15405cp

1.00 FOR BAG OF 10

2.2 uF

JAMECO

93729cp

1.00 FOR BAG OF 10

RESISTORS

 

 

 

 

100 OHM RESISTOR

Radio Shack

271-1108

$1 for 5

10 ResistorsEach type

10 KILO OHM RESISTOR

Radio Shack

 

 

2.2 KILO OHM

Radio Shack

 

 

1.25 MEGA OHM

Radio Shack

 

 

CLOCK

 

4MHz  OSC4

JAMECO

27967 CP

1.99

3

LCD

 

DMC-16207

JAMECO

171715

19.95

1

VOLTAGE REGULATOR

 

 

 

 

LM78L05

JAMECO

51182 CP

0.29

2

MICS.

 

 

 

 

40-PIN ZIF SOCKET

JAMECO

104029

10.95

2

PUSHBUTTON SWITCH

JAMECO

71642

1.49

2

T1-3/4 GREEN LED

JAMECO

104256

0.29

2

6 INCH PROTOTYPING CIRCUIT BOARD

RADIO SHACK

276-170

2.99

2

The Parts
The Sensors: ADXL202AE

Figure1

ADXL202AE is a 2-axis accerlerometer with analog or digital output. The digital sifnals whose duty cycles (ratio of pulsewidth to period) are proportional to acceleration. The duty cycle can be measure by using a microprocessor counter. Algorithm is posted later in this tutorial. The duty cycle period is adjustable from 0.5ms to 10ms by adjusting the resistor attach to Rset.

Acceleration experienced by the ADXL202 may be calculated by the following formular:
Acceleration(g) = (Duty Cycle -Duty cycle at Zero(g)) / (Duty Cycle per g)

if the nominal duty cycle output of the ADXL202 is 50% at zero g and 12.5% duty cycle change per g. Therefore to calculate acccerlation from the duty cycle:

Acceleration(g) = ( ( T1/T2 ) - 50% ) / (12.50%)


Figure 2 Copyright to Nick Vaillant, Spartan Embeded Technology

Figure 3

Duty Cycles: think of this as a square wave, the longer the pulse of the wave is, the higher is that value. And have a counter count how many time of certain interval that the pulse stay high and low will give us the value.

The duty cycle = (pulse interval) / (period).
The period is set using a resistor at T2 (Rset)

Voltage Regulator LM78L05:

The voltage regulator will ensure a correct level of voltage supply to component.
wpe2.jpg (4892 bytes) wpe3.jpg (14262 bytes)
Figure 4 and Figure 5

The LCD: We are using a DCM-16L207 LCD. This LCD has 2 line, 16 character per line display. The x207 has capability of displaying ASCII characters.

Note: The Lcd has port 1 -> 14, the circuit use port 14 -> 11 for 4 bit data transfering, port 6,5,4 for LCD control, port 2 for drive voltage and port 1 for ground. Missing from the circuit is port 3 which drop through a resistor or a potential resistor to controll the contrast of the LCD. Missing this resistor will leave the LCD blank and you won't see anything. I use a 3.5K Ohm for this, anything near it will be ok.


Building the circuit

Figure 6 Copy right of Ross Tsukashima, Circuit Cellar Magazine, issue 154 May 2003

Building the circuit require time and determination. Since the ADXL202 sensor is very small, having a mounted board for this chip will make work easier. I didn't think of it when I build this circuit so you can see that my board is kind of messy. But as long as I have the sensor oriented correclty, this won't affect the performance

Figure 7
Note: As you can see, I have made mistake not connect any resitor to pin3 of the LCD, it take me some delay to figure out this mistake. When debug your circuit, all part are significant. I kind of take that for granted and didn't check the LCD carefully.

The test run

Figure 8 Image display the calibration routine


Figure 9

Figure 10 Show the LCD display (x axis) a 1 G when I stand it vertically to measure earth's natural aceleration


Figure 11 More picture showing test measuring of earth natural acceleration. You could notice that the sensor is ntot there; I decided to put it under the board after 2 or 3 time of debugging.
More picture 1 Picture showing the board measuring Earth's acceleration using y axis.
More picture 2

I have also ran a test in my car(Toyota Camry) with an evaluation board, purchases from Jameco. Showing is a graphic of the data from the test run. As you can see the peek acceleration reach almost 0.439 G. Using the board that i build. the board measure a 0.41 G, which is almost close enough. This error i due to my little cousin who didn't hit the zero button to calibrate the evaluation board before the test run. I had him sitting on the passenger to do the test; he had a thrill time while I drive. Errors are also due to the different traction that the car gets and the amount of pressure I had on the pedal. These factors are not relevant for now.

Test run 1
Figure 12

This show the peek of acceleration is near when I first press on the gas pedal, the test distance is kinda short so I have to let go of the pedal very soon after start, as you can see the decelleration when I let go of the pedal. Accelleration became constant when I completely let go of the gas pedal. Accelleration became negative when I start breaking, there is an oscillation because I don't hold on the break but instead constantly tapping it and let go until the car slow down.

Reverse
figure 13

This image show a silly mistake that I made. I back up to the line and get ready to hit the gas to go forward. Well, I forgot to switch gear, so when I hit the gas, the car decellerate, I when backward. Luckily there wasn't any one arround and there wasn't any wall or poll, otherwise I will be miserable with any dents on my car.


The Programs

You can find the program by downloading it from here,GFORCE3.asm,GFORCE3.hex,GFORCE3.pbp. The author write this program using Picbasic Pro Compiler, which you can find an evaluation copy here PicBasic Complier.

I have acquire a copy of picbasic pro and have look at some of the algorithm on which they use to call the syntax "pulsin" and "lcd". My future plan is to rewrite these algorithm in either C or Assembly.

Final Words

Building this tutorial was a good experience. I get to learn about the ADXL202 sensor, PWM, DCM the LCD, even though there are already syntax in picbasic to implement these processes. I wish to learn those algorithm and rewrite them in C for my own to use.

Best of them all was the testing part. I have a good excuse to see what my car can do. Well, we all know that an old Camry won't do much at the test line.

References
Website
Keith Servic's website
Circuit Cellar
Analog
Picbasic

Publifications
Ross Tsukashima, Circuit Cellar Magazine, issue 154 May 2003
Pic'n up the pace by david Benson
Microcontrol'n Apps by David Benson