Monday, April 21, 2014

Freetronics OLED + MPU6050

This post is a follow-up to my previous post. In here i have shown the setup i did for the OLED and MPU 6050. The only difference is that i have used the MPU 6050 IMU in this case which is a 6DOF MEMS device sporting a triple axis accelerometer and gyroscope. The Teensy 3.1, MPU6050 and OLED are powered by a 400mAH LiPo battery. For lesser power consumption the Gyro and Temperature sensor of the MPU-6050 were disabled and the sensor data was made available at a frequency of 40 Hz - the wake up frequency configured in the LP_WAKE_CTRL register. In this way with only the accelerometer enabled in low power mode, as per the datasheet, the accelerometer draws 140 microamps. The objective was to test how long the configuration will operate (I've got bigger plans for these little circuits....coming soon)

An Arduino UNO powered by a DC adapter to drive the servo on which the MPU-6050 is mounted

The servo setup. The servo arm swipes back and forth 180 degrees to simulate movement and generate data off the sensor board, displaying a neat (oscillating) graph

The LiPo and the charger interface

Birds eye view.

The circuit was operational for 5 hours 21 minutes before the battery ran out.

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...