Another illustration how to control and wire the Rolling Ball Tilt Switch or Ball Sensor along with Raspberry Pi. Ball tilt switch allows you to detect an location or inclination they are small and easy to wired-up it use low power if used it properly see below the diagram. They are usually made of cylindrical or square type surface mounting and it has a conductive balls inside. One end has two conductive elements pole when the sensor is oriented so that that the other end is downwards the ball rolls onto the pole and shorts them or switching the both pole.
Required Components
- Raspberry Pi / Banana Pi / Orange Pi (If your using Banana Pi or Orange Pi See first the GPIO Pins)
- Rolling Ball Tilt Switch
- Solder Less Bread Board
- Jumper Wire / DuPont Wire
- LED (Any Color)
- Resistor 220 Ohms
Wiring Diagram
For C Code/WiringPi
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
/* 14CORE Test Code for: Ball Tilt Switch with C-Code .:+osysso++:` `+yhs/-` `-+s+` `:/+++++++++` .:/++ooo++/-` .ooooooooooo+: :///////////- `odh/` `:y+` /ddhsooooooo+ /hddhsooooydddh sdddoooooosdddy` `////////////` -hds` `sy. +ddy` .ddd: sddy.dddo +ddd- .-----------` `hds :sssss/ -ossssso-yy` `hdd: oddy `ddd/oddd:......+dddo .++++++++++++ +dd` :sdddh` :ydddddddo +y/ .ddd+........ `hddy:.....:yddy.hdddddddddddy+. ```````````` ydy .hddd/+hdddhydddh/.+yo /hdddddddddd. :shddddddddhs/`+ddd:````-yddy- :ooooooooooo+ odh` sdddooyyyyyhddddyy.sy+ ` `......... ``.... ....` ```...` ` `` `` ` -dd+`::::` .:::- /yy. -oos+:-oos+--oos+: /o `+y o/: o:+/ h:`yos /+-`/+/+ s:y/y. /dd/ `+yy: +//+ +/:+ +/:+ -. `/ -:o.:/:`//+- + +:- :`-+:`://: +`o`+. -yds. `/yys- .`-.. .``` ```` .`` ` ` ``` ``-..` ` :ydy/.`````.-/oyhs: `+++oo+oo+:.+-++/-/ooo+o +:o/oo///:+/ .:oyhhhhhhhso:` `. ``` */ #include <wiringPi.h> #include <stdio.h> #define SensorPin 0 #define LedOutput 1 void INOUT(char* ledstate) { pinMode(Rpin, OUTPUT); if (ledstate == "OUTPUT") { digitalWrite(LedOutput, HIGH); } else if (ledstate == "INPUT") { digitalWrite(LedOutput, LOW); } else printf("Error Led Output"); } int main(void) { if(wiringPiSetup() == -1) //If no response from wiringpi.h print { printf("Wiring Pi Initialization Faild"); return 1; } pinMode(SensorPin, INPUT); INOUT("INPUT"); while(1){ if(0 == digitalRead(SensorPin)){ delay(10); if(0 == digitalRead(SensorPin)){ INOUT("OUTPUT"); printf("Detected!\n"); } } else if(1 == digitalRead(SensorPin)){ delay(10); if(1 == digitalRead(SensorPin)){ while(!digitalRead(SensorPin)); INOUT("INPUT"); } } } return 0; } |
For Python Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# 14CORE Test Code for: Ball Tilt Switch on Raspberry Pi # .:+osysso++:` # `+yhs/-` `-+s+` `:/+++++++++` .:/++ooo++/-` .ooooooooooo+: :///////////- # `odh/` `:y+` /ddhsooooooo+ /hddhsooooydddh sdddoooooosdddy` `////////////` # -hds` `sy. +ddy` .ddd: sddy.dddo +ddd- .-----------` # `hds :sssss/ -ossssso-yy` `hdd: oddy `ddd/oddd:......+dddo .++++++++++++ # +dd` :sdddh` :ydddddddo +y/ .ddd+........ `hddy:.....:yddy.hdddddddddddy+. ```````````` # ydy .hddd/+hdddhydddh/.+yo /hdddddddddd. :shddddddddhs/`+ddd:````-yddy- :ooooooooooo+ # odh` sdddooyyyyyhddddyy.sy+ ` `......... ``.... ....` ```...` ` `` `` ` # -dd+`::::` .:::- /yy. -oos+:-oos+--oos+: /o `+y o/: o:+/ h:`yos /+-`/+/+ s:y/y. # /dd/ `+yy: +//+ +/:+ +/:+ -. `/ -:o.:/:`//+- + +:- :`-+:`://: +`o`+. # -yds. `/yys- .`-.. .``` ```` .`` ` ` ``` ``-..` ` # :ydy/.`````.-/oyhs: `+++oo+oo+:.+-++/-/ooo+o +:o/oo///:+/ # .:oyhhhhhhhso:` `. ``` #!/usr/bin/env python import RPi.GPIO as GPIO TiltSensorPin = 11 #Sensor is connected to GPIO 17 LedOutputPin = 12 #Output led is connected to GPIO 15 Led_status = 1 def setup(): GPIO.setmode(GPIO.BOARD) # GPIO will be numbered GPIO.setup(LedOutputPin, GPIO.OUT) # Set LedOutputPin's mode is output GPIO.setup(TiltSensorPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.output(LedOutputPin, GPIO.HIGH) # Set LedOutputPin high def swLed(ev=None): global Led_status Led_status = not Led_status GPIO.output(LedOutputPin, Led_status) # switch led status(on-->off; off-->on) if Led_status == 1: print '14CORE | OUTPUT LED TURN OFF' else: print '14CORE | OUTPUT LED TURN ON' def loop(): GPIO.add_event_detect(TiltSensorPin, GPIO.FALLING, callback=swLed) # wait for falling while True: pass # Do nothing def destroy(): GPIO.output(LedOutputPin, GPIO.HIGH) # Led will turn On GPIO.cleanup() # Rlease or clean resource if __name__ == '__main__': setup() try: loop() except KeyboardInterrupt: # When press control C child program will distroy destroy() |
Wiring The Vibration Tilt Switch / Ball Tilt Switch with RPI