Vadim Tabakman

KY017 Mercury Sensor

Starting to get the hang of plugging these sensors in.  I wanted to do a quick post about the KY017 Mercury Sensor.

I found that it wasn't documented well and even the sensor itself, I wasn't sure which was Gnd, which was data and power.  Given that one pin said '-', I assume that is ground (see I'm learning).  The right most pin said 'S'  But I plugged that into my GPIO13 pin.  Therefore the middle pin, is power so I plugged that into the '+' line.  

What confirmed that it worked, is that I actually plugged into my breadboard with the jumpers.  That means I can move the sensor around.  When the little ball of mercury hit the right side (closest to the board), a light light up on the sensor.

Sensor Pointing Under Horizontal

Sensor Pointing Over Horizontal

Jumpers Plugged In

Notice the Black jumper on the right and it's plugged into the '-' line.  That plugged into the '-' on the sensor.  The Yellow jumper is plugged in the middle pin on the sensor.

Python Code

#!/usr/bin/env python

import os
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(13, GPIO.IN)
GPIO.setup(27, GPIO.OUT)

try:
   while True: 
      if (GPIO.input(13)==0): 
         print "Tilt"
         GPIO.output(27, True)
      else: 
         print "Level" 
         GPIO.output(27, False)
except KeyboardInterrupt: 
   print "Program Cancelled..." 
   GPIO.cleanup()

The Python code, as you can see above has something plugged into GPIO27 for output.  That's a Piezo thing I plugged on.  So basically, when the mercury sensor points down, the Piezo makes a clicking sound,.

Downloads

Download the KY017Sensor.py script - this is zipped.  So unzip it when you want to run it.  To run it :

python KY017Sensor.py

Posted by Vadim Tabakman Tuesday, January 17, 2017 1:12:00 PM Categories: GPIO KY017 Mercury Sensor Python Raspberry PI

Comments

You must sign in to this site to post comments.
Already Registered?
Sign In
Not Yet Registered?
Register