Showing posts with label adxl345. Show all posts
Showing posts with label adxl345. Show all posts

Friday, April 18, 2014

Freetronics OLED + Accelerometer

  531 on Z-Axis, seriously !!

This time i decided to interface the 128x128 OLED display from Freetronics to the Teensy 3.1 and have it display the ADXL 345 accelerometer data as a graph for all three axes. The OLED uses the SPI interface and the library provided by Freetronics which can be found here. The ADXL345 interfaces over the I2C bus. You can choose to use the Wire library or the one provided by Adafruit (which internally calles Wire API anyways) which has a few more features. Interfacing all of the above and getting the data from the accelerometer are pretty simple. Displaying the graph on the OLED was a tad nasty business as detailed below.

As per the wiki, the OLED origin (0, 0) lies in the lower left corner. The Y-axis goes up vertically and the X-axis horizontally. To display the graphs in the center of the OLED an offset of 64 needs to be added to plot the data over the Y-Axis. The data coming from the accelerometer needs to be displayed as a graph in realtime. This data needs to be stored somewhere and represented on the graph. The graph is defined as follows - X-axis plots the sample number and Y-Axis the value of that sample. As we have only 128 pixels along X, the sample buffer will be of size 128 shorts. XSamples[128]. As we are plotting all accelerometer data along the X-axis, all sample buffers will be of size 128. As far as the Y-axis is concerned we have max 64 positive and negative values above and below the main axis. Negative values from the accelerometer will appropriately map to a positive value within the range 0 to 63.



To draw the graph lines without taking too much time for drawing operations the method i used is shown above. In the simplest form it's just drawing a line from the current point to the previous point. Every point on the Y-Axis will have a co-ordinate of the form (sample_number, sample_value). Hence, if we have sample x[0] = 5 and the current sample x[1] = 3 then, the parameters to drawLine API of the OLED will be nothing but (1, 3, 0, 5) or (1, x[1], 0, x[0]) or in general form for a sample s (s, x[s], s-1, x[s-1]). Note that s has to start from a value of 1 and that a certain offset needs to be applied to the Y-axis values to display the graph, if the image above makes any sense, i shall leave that for you to figure out. Yes, i am mean. 

After all 128 samples have been plotted it's necessary to clear the OLED screen to display a new graph. This will cause your display to flicker every 128 samples. I am working on a method to do this without using clearscreen and will post shortly.

The final result is shown in the header image above of the oled displaying the accelerometer data while i shake the breakout board. As it can be seen that the z value looks way too high, yet still being plotted properly. This is because of the fact i am sprinteffing the data into a buffer and the values stick in the buffer like ghosts of previous samples inspite of a memset to 0 operation. Note to self - bust these ghosts...


Sunday, December 1, 2013

An ADXL345 Breakout

I once purchased an Analog Devices ADXL 345 breakout board only to find out that it is incompatible with my breadboard. By that i mean it would fit in such a way that it was impossible to connect wires to one side of it and you had to connect them from below the device which was a messy job. The pic below shows the breakout board i got. (this is the pic after i modified it). As you can see it spans the entire row leaving no room on one side for connections. It made me want to get a larger breadboard, which, for reasons, was out of stock for months from a local dealer.


Another shot of the pins


As it can be seen, the solution was quiet simple. As i was going to use the device in I2C mode, I nuked the INT1, INT2, SD0, VC, CS pins. The board had by default the CS line conencted to Vcc that makes it use the I2C bus instead of the SPI and mounted the breakout on the power rail section of the breadboard as shown. I had to bend the GND pin 90 degrees to ensure a proper fit.


The bridge between the power rails is where the breakout goes into.


The GND connects to the opposite power rail or the Arduino header.

So, in case you happen to purchase this crappy breakout and do not have one of the large bread boards, this is the way to get things running. In other words, have a good look at the breakout, do not purchase it..

Here is a rough sketch in Arduino to interface with this breakout.