Home Code RPI-1031 tilt sensor and Arduino example

RPI-1031 tilt sensor and Arduino example

by shedboy71

In this article we look at a tilt sensor – this time its the RPI-1031 and we will connect it to an Arduino

The RPI-1031 tilt sensor is capable of sensing a change in orientation in four different directions: forward, back, left or right</p

This tilt sensor can be used to detect multiple orientations. Inside the sensor are infrared emitters which are either reflected or not, depending on the orientation of the sensor. By reading the output of the various pins you can easily determine the orientation of the sensor and your project.

Being a tilt sensor this is just a metal ball that rolls around inside the case. The ball is always up against one side even when it is flat.

The sensor needs power and ground, you can then connect the 2 digital pins. These 2 digital pins will output LOW/LOW, LOW/HIGH, HIGH/LOW and HIGH/HIGH depending on the wall it is touching.

Parts Required

 

Name Link
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO
RPI-1031 RPI-1031 Angle Sensor 4DOF Attitude HM Module 4 Direction For Arduino
Connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire
sensor shield Expansion IO Board Sensor Shield

Schematic/Connection

Arduino Sensor
5v Vcc
Gnd Gnd
S1 D1
S1 D2

 

RPI-1031_arduino

 

Code Example

The code for this sensor is really basic, it just checks the 2 pins, to see what side is being touched – The simple function simply takes the 2 digital outputs and returns 0,1,2 or 3 depending on the side.

int tilt_s1 = 2;
int tilt_s2 = 3;
void setup(){
pinMode(tilt_s1, INPUT);
pinMode(tilt_s2, INPUT);
Serial.begin(9600);
}
void loop(){
int position = getTiltPosition();
Serial.println(position);
delay(200); //only here to slow down the serial output
}
int getTiltPosition(){
int s1 = digitalRead(tilt_s1);
int s2 = digitalRead(tilt_s2);
return (s1 << 1) | s2; //bitwise math to combine the values
}

 

Output

Open the serial monitor and you should see something like this, I was moving the sensor around.

Not the most intuitive readings admittedly

3
3
3
2
0
0

 

Links

https://github.com/sparkfun/Tilt-a-Whirl_Breakout

 

 

 

 

 

Share

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More