Loading...
 

ArduinoM0 Tilt Sensor Example

Tilt Sensor

The tilt sensor is essentially a button, activated by its movement rather than a click with your finger.
So these sensors are used to detect movements.
For example, you can place a tilt sensor to an object and then you can monitor its movement.
NOTE:
The Arduino board M0 operates on 3.3V. The maximum voltage that the I/O pins can tolerate is 3.3V. The application of a voltage exceeding 3.3V may damage the board.

Hardware Required

  • Arduino Board
  • Tilt sensor
  • LED
  • Resistor 220 ohm

Circuit

Image
Connect the tilt sensor to the Ground and 3.3V PIN of your board.
Between the 3.3V and the tilt PIN there's a 10K resistance.
Then wire up the 3.3V PIN of the sensor to the Arduino Board PIN digital 2.

Note:
In the picture is shown the circuit realized using an Arduino M0 Pro.

Code

The sketch code defines variables used to control the Tilt sensor and the LED.
There is also other varible used to determine the status of sensor.
The void loop repeatly reads the tilt sensor PIN.
If a movement is detected, it changes the LED status to LOW or HIGH.
Else the Led is turned off.

/*Tilt sensor Example*/
// these two costants are used to define used PINs
const int TiltPin = 2;// the number of the Tilt sensor pin
const int ledPin =  13;// the number of the LED pin

int Tilt_State = 0;// variable used for reading the Tilt sensor status

void setup() {
// initialize the LED pin as an output
pinMode(ledPin, OUTPUT);      
// initialize the Tilt sensor pin as an input
pinMode(TiltPin, INPUT);     
}

void loop() {
// read the state of the Tilt sensor 
Tilt_State = digitalRead(TiltPin);

// check if the Tilt sensor is moved and change turn on/off LED
if (Tilt_State == HIGH) {     
// turn LED on
digitalWrite(ledPin, HIGH);  
} 
else {
// turn LED off
digitalWrite(ledPin, LOW); 
}
}