The ADXL335 is a small, thin, low power, complete 3-axis accel- erometer with signal conditioned voltage outputs.
The product measures acceleration with a minimum full-scale range of ±3 g.
It can measure the static acceleration of gravity in tilt-sensing applications, as well as dynamic acceleration resulting from motion, shock, or vibration.
Here there is a collection of examples for connect the ADXL335 with FoxBoard G20
# Part of this library is copied to http://foxg20.acmesystems.it/doku.php?id=tutorial:4dsystems_oled # Thank to acmesystems for this code # $Id$ import os.path import serial import fcntl, struct, termios, os import time class display: def __init__(self, port="/dev/ttyS3"): self.ser = serial.Serial(port, baudrate=115200, timeout=1, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS ) self.ser.write("U") # Autobaud char self.ser.read(1) def clear(self): """Clear screen""" self.ser.write("E") self.ser.read(1) def line(self, x1, y1, x2, y2,color_msb,color_lsb): self.ser.write("L%c%c%c%c%c%c" % (int(x1),int(y1),int(x2),int(y2),color_msb,color_lsb)) self.ser.read(1) def draw_ascii(self, str , x, y, color_msb, color_lsb,width, height): self.ser.write("t%c%c%c%c%c%c%c" % (str,int(x),int(y),color_msb,color_lsb,int(width),int(height))) self.ser.read(1) def write(self, string, x=0, y=0): """Write string to x,y position""" self.ser.write("s%c%c%c%c%c%s%c" % (int(x),int(y),1,0xFF,0xFF,string,0x00)) self.ser.read(1)
#!/usr/bin/python # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import time import uoled p_1g_adc_value=850 # ADC value at +1g n_1g_adc_value=730 # ADC value at -1g g0_adc_value=790 # ADC value at 0g delta_adc_value=(p_1g_adc_value-n_1g_adc_value)/2 # ADC range between 0g -> 1g uoled_resolution_x=160 # uoled resolution X uoled_resolution_y=128 # uoled resolution Y wave_space=(uoled_resolution_y-4)/3 # maximum excursion of pixels of the waveform graph_x_axis=(wave_space/2)+1 # axis position of the accelerometer X graph_y_axis=(wave_space/2)+2+wave_space # axis position of the accelerometer Y graph_z_axis=(wave_space/2)+3+(2*wave_space) #axis position of the accelerometer Z # Function for reading the ADC def adc_read(channel): if( channel < 0 or channel > 3 ): return -1 else: f = open('/sys/bus/platform/devices/at91_adc/chan' + str(channel),'r') data= f.read() f.close() return int(data) # return ADC channel value # Function to convert the ADC value in +-1g def acc_g(channel): value=adc_read(channel) if (int(value) > p_1g_adc_value): value=p_1g_adc_value elif ( int(value) < n_1g_adc_value): value=n_1g_adc_value return (value-g0_adc_value)/float(delta_adc_value) # Function to convert the "g" to coordinate Y on the display def point_graph(channel): value=acc_g(channel) if(channel==0): center=graph_x_axis # X accelerometer shifted on coordinate graph_x_axis elif(channel==1): center=graph_y_axis # Y accelerometer shifted on coordinate graph_y_axis elif(channel==2): center=graph_z_axis # Z accelerometer shifted on coordinate graph_z_axis if(float(value) >= 0): return center-((wave_space/2)*value) # shifted Positive g else: return center+((wave_space/2)*abs(value)) # shifted Negative g # Function to print the X, Y, Z axis def print_axis(uoled): uoled.clear() uoled.write("x",0,2) uoled.line(8,graph_x_axis,uoled_resolution_x,graph_x_axis,0xFF,0xFF) uoled.write("y",0,7) uoled.line(8,graph_y_axis,uoled_resolution_x,graph_y_axis,0xFF,0xFF) uoled.write("z",0,12) uoled.line(8,graph_z_axis,uoled_resolution_x,graph_z_axis,0xFF,0xFF) def banner(uoled): uoled.clear() uoled.draw_ascii("M",0,25,0x00,0x1F,3,3) uoled.draw_ascii("a",20,25,0x00,0x1F,3,3) uoled.draw_ascii("d",40,25,0x00,0x1F,3,3) uoled.draw_ascii("e",60,25,0x00,0x1F,3,3) uoled.draw_ascii("f",80,25,0x00,0x1F,3,3) uoled.draw_ascii("r",100,25,0x00,0x1F,3,3) uoled.draw_ascii("e",120,25,0x00,0x1F,3,3) uoled.draw_ascii("e",140,25,0x00,0x1F,3,3) uoled.draw_ascii("E",0,60,0x00,0x1F,2,2) uoled.draw_ascii("l",15,60,0x00,0x1F,2,2) uoled.draw_ascii("e",30,60,0x00,0x1F,2,2) uoled.draw_ascii("c",45,60,0x00,0x1F,2,2) uoled.draw_ascii("t",60,60,0x00,0x1F,2,2) uoled.draw_ascii("r",75,60,0x00,0x1F,2,2) uoled.draw_ascii("o",90,60,0x00,0x1F,2,2) uoled.draw_ascii("n",105,60,0x00,0x1F,2,2) uoled.draw_ascii("i",120,60,0x00,0x1F,2,2) uoled.draw_ascii("c",135,60,0x00,0x1F,2,2) uoled.draw_ascii("s",150,60,0x00,0x1F,2,2) uoled.write("FoxBoard",6,14) uoled.write("ADXL335 Sample Test",0,15) time.sleep(3) inc=0 # variable to increase the X coordinate on the display x0=point_graph(0) # initial value of the accelerometer X y0=point_graph(1) # initial value of the accelerometer Y z0=point_graph(2) # initial value of the accelerometer Z uoled = uoled.display() # init uoled display banner(uoled) print_axis(uoled) while 1: # Loop x1=point_graph(0) y1=point_graph(1) z1=point_graph(2) uoled.line(inc,x0,inc+1,x1,0x1F,0x1F) uoled.line(inc,y0,inc+1,y1,0xF8,0x00) uoled.line(inc,z0,inc+1,z1,0x07,0xE0) inc+=1 x0=x1 # update the previous value of the accelerometer X y0=y1 # update the previous value of the accelerometer Y z0=z1 # update the previous value of the accelerometer Z if(inc>159): # refresh "new display" inc=0 print_axis(uoled)
$ wget http://wiki.madefree.eu/download/code/Foxboard/ADXL335/uoled.py
$ wget http://wiki.madefree.eu/download/code/Foxboard/ADXL335/graph_accelerometer.py
$ chmod +x graph_accelerometer.py
$ ./graph_accelerometer.py