Flash

Send to KindleWorking a lot with Raspberry Pi and Arduino stuff lately. The concept of pull-up and pull-down resistors came up quickly and confused me a little at first. So I thought I’d do a little demo of how they work and why th...
Send to KindleWorking a lot with Raspberry Pi and Arduino stuff lately. The concept of pull-up and pull-down resistors came up quickly and confused me a little at first. So I thought I’d do a little demo of how they work and why they are needed. Doing this helped to clarify it for me, so maybe it’ll help you too. Common scenario. You want to set up something that reads an input of a GPIO pin. Say, you want to know when the user is pressing a switch. You set up a simple circuit like so: When you press the button, pin 25 goes high. No press, no high, must be low, right? Well, let’s see… We’ll set up a simple Python script to read the status of pin 25: #! /usr/bin/python import RPi.GPIO as GPIO import time PIN = 25 GPIO.setmode(GPIO.BCM) GPIO.setup(PIN, GPIO.IN) while True: if GPIO.input(PIN) == GPIO.HIGH: print("pin is high") time.sleep(.1) else: print("pin is low") time.sleep(.1) When I run this, I get 20-30 lines saying pin 25 is high, then 20-30 saying it’s low, back and forth, without touching the button. Sure enough when I press the button, it stays high, but apparently the definition of “low” is not “not high”. After too many years in the highly binary world of software programming, some of us delving into hardware may be surprised to learn that a lot of electronics operate on “tri-state logic” as opposed to simple binary. Here, an input can be high, low, or floating. In this particular circuit, high means connected to 3.3 volts, low means connected to ground, and floating means… neither or somewhere in between. Actually, due to the sensitive nature of tiny electronics, system components can pick up various signals that create slight fluctuations in what it is reading. It’s rarely if ever going to pick up exactly 0.000 volts just because it’s not connected to 3.3 volts. So we need to force the issue and say, “Yo! You are LOW!” My first thought on how to do this would probably have been to do something like this: When the switch is in one position, pin 25 is connected directly to ground, sending it solidly low. When the switch is changed, it connects 25 to 3.3 volts, making it high. This will work just fine, but it’s a bit overengineered as it turns out. How about if we go simpler and just connect pin 25 to ground and leave it that way, like so: (Note: DO NOT ACTUALLY BUILD THIS CIRCUIT!) Now, when the switch is open, pin 25 is undoubtedly low. But we have a problem that when you do hit the switch, you now have a short circuit directly between 3.3 volts and ground. Not good. Again, do not do this. The solution? Throw a resistor in there: Now, when the switch is open, pin 25 is connected to ground through the 10K resistor. Even with the resistor though, it’s enough of a connection to make it solidly low. It’s not going to fluctuate. Then we hit the switch, connecting the lower part of the circuit to the 3.3 volts. Because we have a fairly high resistance there, most of the current is going to go to pin 25, sending it solidly high. Some of it is also going to go to ground via R1. But Ohm’s Law tells us that 3.3 volts divided by 10,000 ohms will let about 0.33 milliamps in. That’s not going to break the bank. So in this circuit, R1 is called a pull-down resistor because in its default state it pulls the signal down to 0 volts or a low state. Now let’s swap things around a bit: Now pin 25 is hard-wired to 3.3 volts through R1, making its default state high. However when the button is pressed, pin 25 will be directly connected to ground, sending it low. Because the resistor pulls pin 25 up to 3.3 volts and a high state, it’s now known as a pull-up resistor. So, which one do you use? It depends on whether you want your default signal to be high or low. Hope that helps. Send to Kindle
about 7 hours ago
Learn how to create and manage Adobe Texture Format files using utilities provided by Adobe.
Learn how to create and manage Adobe Texture Format files using utilities provided by Adobe.
about 15 hours ago
Send to Kindle I was looking for some Raspberry Pi project to do that not only used external hardware (even if that means only an LED for now), but also reached out into the net to deal with some kind of real time data. I ran into this t...
Send to Kindle I was looking for some Raspberry Pi project to do that not only used external hardware (even if that means only an LED for now), but also reached out into the net to deal with some kind of real time data. I ran into this tutorial on adafruit about checking your gmail with a Pi and lighting up an LED based on whether or not you have new mail. This was along the lines of what I wanted to do, but had two drawbacks. One, I don’t use gmail anymore, and two, it uses a python library called feedparser, which is actually an RSS parser. It only works because apparently you can access your gmail as an RSS feed or something. I wanted to do the same thing, but with any email service that supports, say, IMAP4. A bit of digging around and I found that Python has an imaplib library that can directly access any IMAP4 based 12mail account. This is included by default in the Python distro that comes with Raspbian. So no further setup there. It took a bit of fiddling around with the docs, both of the library itself and the IMAP4 specs, but I figured out the basic sequence. The library docs are here: http://pymotw.com/2/imaplib/ And the IMAP4 spec is here: http://tools.ietf.org/html/rfc3501.html First, you create an IMAP object with one of the following lines, depending whether you need SSL or not. My server does need it. imap = imaplib.IMAP4(server, port) or imap = imaplib.IMAP4_SSL(server, port) Then, you log in: imap.login(user, password) You then need to go into select mode to select IMAP folders and messages. imap.select() Then you can do a search. By default, you’ll be searching in your inbox, but there are methods to change folders as well. Search criteria are pretty powerful, but I just wanted to see how many unread messages I have. This does that: type, data = imap.search(None, "UNSEEN") The first parameter is the encoding. Using None will return messages with any encoding. Second param is the search criteria. See the IMAP4 spec linked above for a comprehensive list on what you can search for. This will return a type, data tuple of strings. The type will be “OK” or “NO” depending on success of the call. Note, even if it returns no unread messages, you’ll still get “OK” here, with an empty string in the data. The data will be an list of strings. Actually, it seems to generally be an list containing a single string. This string is a space delimited list of numbers. These numbers are ids of the messages returned by the search. It’ll be something like this: “1 2 3 4 5″. You can split this into a list of individual ids like so: messages = data[0].split() And the length of this list tells you how many messages the search returned. Zero means no new mail. One or more and you have new mail! Fire up an LED! Here’s the full program I wrote: #! /usr/bin/python import imaplib, time import RPi.GPIO as GPIO GREEN_PIN = 25 RED_PIN = 24 class MailChecker: def __init__(self, server, port): GPIO.setmode(GPIO.BCM) GPIO.setup(GREEN_PIN, GPIO.OUT) GPIO.setup(RED_PIN, GPIO.OUT) GPIO.setwarnings(False) try: self.m = imaplib.IMAP4_SSL(server, port) except: self.do_error("Unable to contact server") def do_error(self, error): # maybe flash another error LED print(error) exit() def log_in(self, user, password): try: self.m.login(user, password) except: self.do_error("Unable to log in") def check_mail(self): type, data = 0, 0 try: self.m.select() type, data = self.m.search(None, "UNSEEN") except: self.do_error("Unable to check messages") if type == "NO": self.do_error("Problem checking messages") self.report(data) def start_checking(self, interval): while True: self.check_mail() time.sleep(interval) def report(self, data): message_count = len(data[0].split()) if message_count > 0: print("You've got %i new messages" % message_count
1 day ago
3D Zoom One is a stack of photos with dragger control and previous/next buttons. Configure settings in XML for image size, description width and height, tweener transition effect, number of photos displayed in stack form, HTML-CSS descri...
3D Zoom One is a stack of photos with dragger control and previous/next buttons. Configure settings in XML for image size, description width and height, tweener transition effect, number of photos displayed in stack form, HTML-CSS descriptions for photos, etc.Views: 77 | Downloads: 4Added: Tue, 21 May 2013 17:13:11 +1300
2 days ago
Learn how to create PDF versions of your folios, including interactive states, to share across your organization for review and feedback.
Learn how to create PDF versions of your folios, including interactive states, to share across your organization for review and feedback.
2 days ago
Get a first look at the next generation of Dreamweaver, with new features for visual CSS Designer and more.
Get a first look at the next generation of Dreamweaver, with new features for visual CSS Designer and more.
3 days ago
Send to KindleRecap As stated in my previous post, the plan was to hit a button that’s connected to my Raspberry Pi, which will trigger Winamp running on my pc laptop to play/pause. This without the Pi being physically connected to...
Send to KindleRecap As stated in my previous post, the plan was to hit a button that’s connected to my Raspberry Pi, which will trigger Winamp running on my pc laptop to play/pause. This without the Pi being physically connected to the pc – all via wifi and my local home network. The hardware setup Raspberry Pi with USB wifi, a small breadboard, 100k ohm resistor, a pushbutton and some hookup wire. GPIO pin 25 goes to one side of the switch, 3.3 volts to the other. You hit the switch, pin 25 gets juice and goes HIGH. The 100k ohm resistor is also hooked to pin 25 on one side and ground on the other, connecting 25 to ground when the switch is not connected, ensuring it is in a LOW state. For those of you in the know, this is a basic pull down resistor. If you’re not familiar with the concept, as I was not last week, this is a good explanation: http://www.resistorguide.com/pull-up-resistor_pull-down-resistor/ The Python The Raspberry Pi is running a simple program written in Python: import url
8 days ago
Send to KindleBack story I wanted a media center. I priced out cases, power supplies, hard drives, cheap motherboards, etc. and figured I could build one for a couple hundred bucks or so. Then I heard about people using Raspberry Pi̵...
Send to KindleBack story I wanted a media center. I priced out cases, power supplies, hard drives, cheap motherboards, etc. and figured I could build one for a couple hundred bucks or so. Then I heard about people using Raspberry Pi’s as media centers. Did a bit of research and got me a Pi and set up OpenElec on it. Plugged in an old 500 GB USB drive. I had a media center for about a quarter of what I was going to pay. Thing is, getting the Pi set up was so much fun, I decided to buy another one just to play with. A week later I bought an Arduino Uno. I even pulled my barely touched Beagle Board out of cold storage and plugged it in. I’ve been having a blast the last couple of weeks playing with all these new toys. Bought a bunch of electronic parts, breadboard, hookup wire, gathered up some other junk I had stashed here and there. Back back story When I was 11 or 12 years old, one of my class mates brought in this Radio Shack 75-in-1 Electronics kit and did a little presentation on how to hook u
8 days ago
Learn how to use the GameInput API to communicate with several different types of devices.
Learn how to use the GameInput API to communicate with several different types of devices.
10 days ago
Get the latest information for IT or administrative professionals who manage the installation or use of Flash Player for multiple users in a controlled environment.
Get the latest information for IT or administrative professionals who manage the installation or use of Flash Player for multiple users in a controlled environment.
13 days ago