Microcontroller fun?

I’m going back to work soon! (I think) Yay!

In other news I’m having trouble getting an HC-SR04 ultrasonic distance sensor working with the Raspberry Pi. I refuse to sit there and poll for the signal back. I’d rather just have a thread dealing with that (actually I’m tinkering with gevent at the moment) and just be able to get the distance data when I need it in another thread.

Here’s the issues I’m having:

1) No matter how I set it, if use any of the asynchronous event management from the RPi.GPIO callback (which is a separate C thread). I’ve solved this by having another thread deal just with sending triggers so that I know there is always a recent distance measurement available when it’s needed. It’s a bit of extra overhead, but not enough that I mind and it means that any measurement is fresher.

2) As I’m doing more it’s more and more likely that the interrupt misses the falling edge of the output signal.


1) Code my own interrupt routine to replace RPi.GPIO’s in C.

2) Dedicate a thread to polling for rising and falling edges on the signal pin.

3) Outsource this, and possibly some other functions, to a dedicated microcontroller.

There are pros and cons to all of the solutions. 1) I was really trying to code the entire thing in Python… because challenge, not for any practical reason. 2) Okay, not really any reason not to do this… I haven’t investigated it a lot (at all) to see how much overhead this would use. 3) Well, downside is that I really want to have all of the code out there and simple to use.

Don’t know which way I will go right now. I’m leaning toward #3 because, well, I actually think outsourcing it to an MSP430 with really good power management would be cheaper from a power budget standpoint… Might do 2 and 3 with a config option to switch so people can still use the code without extra hardware if they want.

Fun with LED Bar Graphs (LED Binary Counter)

So I’m working with i2c, I’ve literally dredged through my boxes of parts and I came across a breadboard with an MSP430 and a pair of PCF8574s wired up to it. Since I’m playing with the Raspberry Pi right now I pulled the MSP430 off and plugged the Pi in.

A few minutes later I had a couple of little python programs running. This is my favorite (but least useful). I’ve commented out the more useful code that reads a DIP switch on one and simply writes it to the other. Below the code I have some comments.

import smbus
import time

  bus = smbus.SMBus(1)
except IOError:
  bus = smbus.SMBus(0)

#while True:
#  switch = bus.read_byte(0x21)
#  bus.write_byte(0x20, switch)
#  time.sleep(.1)

bus.write_byte(0x20, 0xFF)
while True:
  for i in range (0, 255):
    if i == 0:
      for x in range (0, 3):
        bus.write_byte(0x20, 0xFF)
        bus.write_byte(0x20, 0x00)
    bus.write_byte(0x20, ~i)

The code starts out importing smbus, which is the library that makes this all easy, and the time library for the simple time.sleep().

Beyond that I have a code snippet I’m tinkering with, most people differentiate between Pi models by accessing a file that has which revision it is… I’m going to work out if simply attempting to open each bus option and catching the exception is more efficient. It’s certainly simpler, and I feel, more robust.

Below that is my original code that read the switch and simply output the byte to the other port expander.

After the comments is what I really like, simply because it is pretty. I’m actually having a bad enough day that I’ve spent it in bed, so this is set up on my night stand and I am enjoying just watching it count.

According to the datasheet it is slightly preferred to sink current with the PCF8574, this is very common so generally simply a good practice so sending 0xFF actually blanks the LED bar graph. Exactly how I’ve attached it I’ll go in to in a different post… because reasons.

I start off blinking it on and off, 0.2 seconds was most aesthetically pleasing to me, and 3 times seemed nice.

After blinking it simply starts counting and sending the current count out to the expander. You can note the ~ which actually sends the opposite (from a bitwise sense) to the expander since it is sinking current. By doing that it counts up to 255 (the 8 connected LEDs on), then flashes and starts over.

Flashing LEDs often make me very happy, I don’t know why. If they make you happy, enjoy this binary counter.

*Connect your LEDs with an appropriate resistor to your voltage and their spec. This works well with 8 individual LEDs or an LED bar graph like I happened to have.

First Post!

I never know what to write on a first post. It should be deep. It should be thought provoking. It should set the tone for everything to come on the site.

Yeah, well, I suffered a head injury at work and am now mostly well but still waiting on the molasses slow workers comp system in Nevada to let me see a neurologist to get cleared to go back to work.

I mostly started doing some coding to see if, well, I could see out of my right eye. It wasn’t working for more than a couple of minutes looking at text.

Pulled out the Raspberry Pi I got a few weeks ago, dredged up a bin of parts, and started doing things like logging the temperature here and making some lights count in binary.

In the process I found that there didn’t seem to be a repository for a lot of the basic library code I wanted, at least not in Python that I wanted to sharpen my skills at. Most of the things I found were either very basic or very specialized. And what I found never really did what I wanted so I’ve written a lot of little bits of glue and thought I’d share them.

And, oh, yeah, I got the Pi originally to play around with OpenCV and build a robot, so I’ll share some of that when I get around to it, too.

Yup, that sets a good, hopefully not as sarcastic as it sounded in my head, tone for things to come. Not so deep, though, but it may never be.