Vadim Tabakman

KY017 Mercury Sensor 

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

Getting an LED to Light Up 

Getting an LED to Light Up

I'll be the first to admit that it's been many many many years since I've done anything to do with electronics and I can't read the simplest electronics diagram if my life depended on it.  This is actually one of the reasons I'm messing around with the Raspberry Pi.  I want to keep my brain active since learning another language (spoken.. not programming) is beyond my limitations.  "Limitations are the shackles we bind to ourselves".  Who said that??? - I'm showing my grandpa'dness.

Last night I was googling around all over the place to get my head around how to get a simple LED to light up using the Raspberry Pi.  It doesn't sound hard, and it's probably the first thing you should do.  It's like the Hello World of electronics.

Firstly, I should tell you what my set up is.

  • Raspberry Pi 2
  • Breadboard
  • T-Type GPIO expansion board
  • Ribbon cable to connect the expansion board to the Raspberry Pi.
  • Red LED
  • 470ohm Resistor
  • 3 jumper leads (red, white an black)

Those are the minimum that I found was needed to get this to work.  I'm going to try to explain why it plugged in the way it is, but I want you to take note that I'm learning here.  So it's possible I'm doing something wrong (hopefully I won't electrocute myself).  If you see I'm doing something wrong or don't get it, let me know.  I'm keen to understand this stuff better.

Making sure there's Power and Ground to the Breadboard

I'm making this the first part of this post, because this is what I didn't know and I woke up this morning with an epiphany.

Feel free to click on the above image, to get a closer look.

Firstly, when you plug in the T-type expansion board into the breadboard (with the ribbon), I was under the wrong impression that somehow the left and right side of the board now had power and ground all connected up, under the covers.  That was a wrong assumption.

As you can see, I have a White jumper connected to Pin 1 (3v3 power) and it goes to the + (red) column.  That sends power down that column, so you can plug things into it now.

I also have a Red jumper that connects the Gnd (ground) pin to the - (blue) column. This is what I was missing as I thought I didn't need to do this.

LED

The LED you can pretty much put anywhere, as long as it's not on the same row.  The rows, being the number set of holes.  What I mean, is that your LED can't take up pins D35 and E35.  One row needs to be for the positive (anode) and the other for the negative(cathode).I've put my Red LED with the anode on E33 and the cathode on E34.  So now my row 34 is the negative (or ground) and row 33 is the positive (power).

For those who are learning, like me, the anode (+) is the long leg of an LED and the cathode (-) is the shorter leg.

Resistor

I've read a bunch about how you shouldn't plug the LED directly to the power with a resistor, because it will try to drag in as much power as possible and burn itself out.  I actually did connect it up without a resistor and it worked, but I don't recommend this as I did it while I had no idea what I was doing.  Which I still feel is the case :).

I took the 470 ohm resistor that came with my kit, but you can use a smaller one since we are just dealing with an LED.  One of the ends of the resistor is plugged in to the cathode row.  In the picture, it's plugged into A34.  The other end of the resistor is plugged into the - line (to ground it).

GPIO17 pin

The final electronics piece of the puzzle, is connecting the LED to one of the GPIO pins, because I want to write some code to talk to it.

In the image above, I have a Black Jumper connected to the anode part of the LED (pin C33.. it could also be A33, B3 or D33)  The other end of the jumper is connected to B6 which is the GPIO17 pin.

Code to turn the LED on and off

Since I'm running Raspbian (linux) on the Raspberry Pi, the simplest way to communicate with the pins is using Python.  Now I'm not a Python guru.  I'm learning it as I go, so it's not going to be pretty. 

Turning on the LED

import RPi.GPIO as GPIO

pinNum = 17

GPIO.setmode(GPIO.BCM)

GPIO.setwarning(False)

GPIO.setup(pinNum, GPIO.OUT)

GPIO.output(pinNum, GPIO.HIGH)

Turning off the LED

import RPi.GPIO as GPIO

pinNum = 17

GPIO.setmode(GPIO.BCM)

GPIO.setwarning(False)

GPIO.setup(pinNum, GPIO.OUT)

GPIO.output(pinNum, GPIO.LOW)

Conclusion

I don't think this is too complicated, but like I said, I'm still learning and I could be doing something wrong or describing it incorrectly.  Please let me know if either is true as I want to understand this stuff and I also want to put it in a way for other beginners/newbies like myself to learn it easily.

A little addon to this. I installed Ninja-IDE to write my Python.  It seems pretty good for my needs.  That way, I can write the Python code and it gives me an idea if I totally messed something up, like syntax.  Then I save the files, eg ledON.py and ledOFF.py and I can run them with something like this:

python ledON.py

and

python ledOFF.py

Posted by Vadim Tabakman Wednesday, January 4, 2017 7:58:00 AM Categories: Breadboard GPIO GPIO17 Jumpers LED Ninja-IDE Python Raspberry PI Resistor T-Type Expansion