the output of accelerometer sensor adxl335 connected to raspberry pi is 1. How to get the output in decimal format

2.5k Views Asked by At

I have written the below program in python and tried to read tthe accelerometer sensor output. When I print the output of the accelerometer I am getting 1. Please advice me how to get actual output in digits to know the exact tilt of the accelerometer.

import RPi.GPIO as io # import the GPIO library we just installed but call it "io"
import time
io.setmode(io.BCM)
io.setwarnings(False)

x_axis = 17 # this is the GPIO number our accelerometer's x axis is connected
y_axis = 27 # this is the GPIO number our accelerometer's x axis is connected
z_axis = 22 # this is the GPIO number our accelerometer's x axis is connected

io.setup(x_axis, io.IN) # initialize receiver GPIO to take input
io.setup(y_axis, io.IN) # initialize receiver GPIO to take input
io.setup(z_axis, io.IN) # initialize receiver GPIO to take input

try:
    while True:
        x = io.input(x_axis)
    if x:
        print"x-axis ="
        print(x)
        time.sleep(1)

        y = io.input(y_axis)
    if y:
        print"y-axis"
        print(y)
        time.sleep(1)

        z = io.input(y_axis)
    if z:
        print"z-axis"
        print(z)
        time.sleep(1)

except KeyboardInterrupt:
    print("Received Interrupt")


output

pi@raspberrypi /opt/embedded $ sudo python accelerometer.py
x-axis =
1
y-axis
1
z-axis
1
x-axis =
1
y-axis
1
z-axis
1
x-axis =
1
y-axis
1
z-axis
1
2

There are 2 best solutions below

0
frazelle09 On

You might consider Joan's short programs for a slightly different IMU as posted here

https://www.raspberrypi.org/forums/viewtopic.php?f=44&t=139584&p=949755#p949755

Have a great afternoon! :)

0
abhishek kotkar On

Actually ADXL335 is an analog sensor and raspberry pi cannot read analog values it does read output as high or low to get exact values of sensor you should use ADC(analog to digital convertor) connected with raspberry pi or you can use Arduino board which can read continuous valued output of sensors