Hidden Links

Playing with Triple-Axis Accelerometer and Ski Free

Roeurn Tourn and Anthony Ma



Purpose of Experiment

The purpose of this experiment was to interface the triple-axis accelerometer with the Nexys-2 board and output acceleration readings on the LCD screen.


System Requirements

In order to attain the "B" level of completion the values from the sensor must be read into the board and displayed on the LCD. In order to attain the "A" level of completion the LCD must show the calibrated acceleration values from the sensor.


System Specification & Architecture

The "Triple-Axis Ski Free project consists of the accelerometer sensor, 2 ADC's, an external LCD, and the Nexys-2 board. Additional functionality was brought in so that the accelerometer could be used to play the computer game "Ski Free." The additional functionality required a custom made circuit, a modified computer keyboard that hooked up to a computer in order to play the game. The minimum hardware configuration for the project consists of a MicroBlaze softcore processor, program memory, 3 SPI's for the ADC's, and general purpose I/O peripherals to connect Mircoblaze to the LCD data and control buses and to use the LED's as a debugging tool. The codes uses a for-loop delay that allows the acceleration to refresh at a rate of 10 times a
second, and LCD refresh every 1 second.


System Integration

The sensor project uses 1,585 out of 9,312 slices for a utilization of 17%. The code size was approximately 5.104 kilobytes's.


Testing Procedure

The first thing that was done was testing the sensor directly, independent of the Nexys-2 board. A DC power supply was used to power up the sensor and scope probes with an oscilloscope were used to analyze the voltage outputted by each of the 3 axes and how gravity affected each axis. Next, an individual ADC was interfaced with the Nexys-2 board. A DC power supply was used to send an adjustable known voltage into the ADC and the LCD outputted the value of these readings. After that the accelerometer sensor was wired into the ADC's and acceleration values were observed on the LCD. Using the maximum and minimum values read from the sensor using the ADC's, functions were created to calibrate the axes so that they they would display a value in the range of approximately 100 to -100 on the LCD. Testing was done by observing the minimums and maximums readings on the LCD due to the force of gravity.


Each axis reading from the ADC will produce a linear output, however these outputs vary range. To compensate for this the raw output from the ADC must be calibrated. The calibration process consists of reading positive gravity and negative gravity. With these values a transformation function was generated that  took the ADC output values as in input and returned a new range from -100 to +100. These fuctions exist as function calls that was included in the calibration.h file.

Encountered Problems

One of the problems encountered early on was the issue of using one analog-to-digital converter chip to sample the acceleration in two axes. The function that was written by the lab tutor was meant to only utilize one of the two available ADC's on one PMOD. Because the project required three acceleration readings, the original adc_get() code was modified so that it would simultaneously sample the acceleration in the x and y axes. These two readings had to be done simultaneous because the wiring of the PMOD has the CS pins of the two ADC's tied together. Three SPI's were created to read the acceleration in each axis, but the x and y axes SPI's shared a CS and SCLK pin so those outputs were implemented in one of the SPI's and not the other in order to please the compiler.

Another problem that was encountered while working on this project was the issue of converting integers to characters. In order to write the accelerations to the LCD, the integers from the adc_get() first had to be converted into arrays of characters in order to be accepted by the LCD_write() function. The C standard library has a function called itoa() that coverts integers to chars but the library was too large to import onto the board. In order to get around this problem, the itoa() function and the function that it uses, reverse(), were found on Wikipedia and the code was brought into the file rather than imported from the standard library. [1]


3-axis Accelerometer

The Sparkfun accelerometer uses the ADXL335 accelerometer chip. Inside the chip there are micromachined structures, attached to a polysilicon spring, suspend over a silicon wafer. The chip measures acceleration by the deflection of the structure, via the capacitance change between the structure and the wafer. This system produces a maximum output of 3g, and can survive a shock at about 10,000g. The Sparkfun board, includes the recommended filtering and decoupling capacitors, and breadboard size through-hole solder points.

The device operates on 1.8V-3.6V. The Nexys board provides 3.0V. The chip outputs analog voltage values that update at 1600Hz for the X and Y axis, and 550 Hz for the Z axis. The analog voltage range for 0g is around 1.5V.

Although the rate of change Vout of each axis is a straight-line function, their min and max values are different for each axis. Also because output sensitivity (scaling factor) varies proportional to the supply voltage, and all datasheet values were generated for Vs at 3.6V this project operates at 3.0V. All these factors make a calibration function essential.


For the Nexys board to read the analog values an ADC is required. This project use the Digilent PmodAD1 ADC. The ADC communicates via an SPI interface. It is note worthy that this SPI is slightly different from what is taught in class. Each Digilent board mounts two ADCs that share a single chip select line, and clock input. This means that the SPI clock of the X SPI is also clocking the Y ADC. Reading the Y ADC requires some adjustment of the SPI function. The adc_get() function works by following these steps: [2]
1. Set mode, inhibit master, disable SPI
2. Write DUMMY data: Send lower byte to DTR
3. Disable SSR: Disconnect ADC
4. Set mode, inhibit Master, enable SPI Start the data transfer
5. Enable SSR: Activate ADC
6. Set mode, Uninhibit Master (UNFREEZE)
7. Poll TxEmpty waits for transmit to finish
8. Inhibity Master (FREEZE)
9. Write new DUMMY data: Send lower byte to DTR
11. Poll TxEmpty waits for transmit to finish
13. Disable SSR: Deactivate ADC
14. Set mode, inhibit Master, disable SPI



To accomplish this project 2 PmodAD1 boards are required to read 3 analog voltage readings. There must also be 3 SPI hardware IPs installed on microblaze to read each of the 3 axes. The Z SPI is straight forward, it uses an unmodified SPI function call to read 1 Z ADC on 1 Pmod board. Reading the X ADC, and the Y ADC is slightly more involved. Both X ADC and Y ADC are on the same Pmod board. The X SPI Slave Select (referred to as Chip Select on the Pmod) and the X SPI Clock are patched into the Y ADC.

To do a reading, both X ADC and Y ADC are read at the same time. Each line of the standard X SPI function are repeated with the Y SPI for the Y ADC. Despite the fact that there are no external uses of the Y SPI clock, or Y SPI Slave Select, these inputs must be included in the Y SPI IP. The SPI function then also must be adjusted to receive 2 dereferenced pointers.

Ski Free function

Ski Free is a unique game in that it only requires 3 buttons to play: left, down, and Right. There are only 5 possible direction states: left continuous, 45 degree left, down, 45 degree right, right continuous, and stop. To move left is a continuous press of the left button; same is for the right and down directions. However, the left 45 degree, and right 45 degree direction states require a single direction press (left / right) and a single down press in that order. To accomplish thisthe skiifree function declares "direction" a static int. This allows the skiifree function to detect a change in direction and thus executes the 45 degree function only the first time they occur.

Three values representing x, y, and z, are passed into the skiifree function. Once the device is mounted with the z-axis pointing in the up direction, x-axis pointing left, and y-axis pointing  toward the user, tilt of the device can be defined from the 3 acceleration measurements. The 6 basic tilts used in this project all maintain a distinct acceleration profiles. And they are as follows:

Left continuous:
• X<-50
• Y<25
• Z<50

Left 45 degree:
• X<-40
• Y<-40
• Z<85

• X<85
• Y<-30
• Z<20

Right 45 degree:
• X>50
• Y<-40
• Z<85

Right continuous:
• X>50
• Y<25
• Z<50

 Keyboard interface

Most keyboards house a control board located in the upper right hand corner. The keys on the keyboard exist as two plastic films with thin conductive traces silk screened on the inner layer. These two conductive circuits are separated by a thin non-conducting plastic insulator. Mechanically pressing a key, will push the conducting top layer through the hole in the insulating middle layer, to make electrical contact with the conductive trace on the lower layer. All these traces terminate onto exposed contacts at the controller board. By connecting two
specific points on the controller board, a "key press" can be simulated.

An N-type MOSFET is used as a transmission gate between these two specific points. By saturating the MOSFET (with a positive input voltage) an electrical connection is made simulating a key press.

It is important to note that when selecting the proper transistor that the body must not be connected to source. The body must be connected to ground which is electrically distinct from the drain and source nodes in our circuit. The N-MOS chip used in this project is the CD4007UBE (from the EE347 Lab kit); this chip only provides 2 transistors that do not connect the body to source or body to drain. Thus two chips are required to implement three keys.

It is important to note that the keyboard should be connected to the target computer prior to disassembly of the keyboard. Some older keyboards may not be recognized instantly by the computer. The computer may require that certain keys be pressed to help identify unknown keys, this action if far easier if the keyboard is still assembled.

The last major obstacle is mapping a key press to the two corresponding connector on the controller board. This can be done by retrieving the upper and lower plastic trace and placing one probe of an Ohm-meter on the desired key's electrical trace, and testing the connector in the area on the plastic where it makes contact with the controller board. This step requires careful documentation and patience.

Testing the keyboard interface can be done in with the MS Excel program running. This shows that any accidental wiring (shows up as key presses). For example, during testing, an expected left cursor move, typed “0” to the screen. Also make sure the 4007 chip is powered and the input gates grounded. A floating gate will cause unwanted key presses. Also remember that while individually testing each gate for proper operation, remember to un-ground the gates, so that power is not shorted to ground.


The triple-axis accelerometer was successfully interfaced with the nexys-2 board using the 2 ADC's. Acceleration values for all three axes were read and calibrated, using a linear model function, before being outputted to the LCD. After that, the Nexys-2 board was interfaced with a modified keyboard circuit so that the accelerometer could be used to control the computer game "Ski Free." This experiment utilized the SPI interface and ADC PMOD which were talked about in lecture. The experiment also utilized the LCD, similar to when it was used in project 1.