Monthly Archives: February 2013

PIR sensor from TAUTIC.COM

pirA few days ago, I heard that Jayson Tautic (@tautic), manufacturer and purveyor of a interesting electronic prototyping goodies, had put up an interesting offering: a small PIR (passive infra red) motion detector. I’m always up for a new sensor to play with, so I ordered a pair for fun, and they arrived on Friday.

It’s a cute little package, much smaller than I expected, about 8x10mm. It has three pins, but didn’t come with a datasheet. Some queries among my buddies on the #tymkrs channel on afternet turned up this datasheet which appears to be the part. That was good enough to sort out the pinouts: the pin labeled + is the supply voltage, the pin on the opposite side is ground, and the middle pin is the sense pin. When the sensor detects motion, that pin goes high, and remains so for two seconds after the motion halts. And that’s about it.

It was tremendously simple to wire it up to an Arduino for testing. I wired the sense pin to pin 8, and then used this simple sketch…

[sourcecode lang=”cpp”]
const int pin = 8 ;
const int ledPin = 13 ;

void
setup()
{
Serial.begin(9600) ;
pinMode(pin, INPUT) ;
pinMode(ledPin, OUTPUT) ;
}

void
loop()
{
Serial.println("NO MOTION DETECTED") ;
digitalWrite(ledPin, LOW) ;
while (digitalRead(pin) == LOW)
delay(100) ;
digitalWrite(ledPin, HIGH) ;
Serial.println("MOTION DETECTED") ;
while (digitalRead(pin) == HIGH)
delay(100) ;
}
[/sourcecode]

It will turn on the LED wired to pin 13 when motion is detected, and also prints a message to the serial port. Simple.

I haven’t done any testing with this, but it appears they might be sensitive enough to detect the motion of my cat, which seems like it could be interesting. Stay tuned.

Using the Raspberry Pi as a wireless webcam server…

webcamThe other day, I was walking around in Fry’s Electronics, and noticed that they had HP HD-2200 webcams on sale for a mere $6. I thought to myself: hey, even if the camera is crappy (and it is) that is simply too cheap to pass up, and grabbed one. Last night, I decided to try to pair it with my Raspberry Pi and the WiPi dongle, and see if I could make a simple webcam that I could move around. Over the next 30 minutes or so, this is what I came up with.

The driver for the webcam already existed. The USB driver lists the maximum power from this webcam at 200ma, which seemed modest enough, so I configured my Raspberry Pi with the WiPi and webcam plugged in directly, without any powered hub. Technically, the combination of the WiPi and the Pi might be over the reasonable limit, but I have seen others do a similar setup, so I decided to step boldly. This is nice, because it means that you just have one plug, and the resulting package is quite compact and mobile.

Experimentation using ffmpeg to read from the v4l2 device showed that the camera needed to capture a few frames before the automatic exposure would yield a decent image, so I grumbled a bit and experimented. After 10 minutes of inconsistent results, I recalled hearing about a different but simple webcam server program called fswebcam. It’s a simple little program, and was in the package repository, so a simple “sudo apt-get install fswebcam” and I had the software installed. It’s got a pretty good man page too. Fswebcam doesn’t stream video, but it can do one-shot and periodic captures, and has a lot of the essential features that I wanted, including capturing and skipping a bunch of frames, and capturing and averaging a bunch of frames for output.

This morning, I left my camera aimed at the bed in our guest room, which is where my cat Scrappy likes to take his naps. I hoped that later in the day, I’d be able to get a picture of him. The room has some direct sun during the day, which makes for some harsh lighting, which makes the picture pretty unimpressive.

output

Nope, he wasn’t napping there. Or was he? I loaded the image into gimp, and stretched the contrast with the Levels adjustment:

scrappy

Ahah! Hiding next to the wall! (That’s a cloud painted on the wall above and to the right of him).

Pretty neat.

Oh, incidently, I didn’t have an http server installed on the Pi, but then remembered that it does have python. If you run “python -m SimpleHTTPServer” it will create a webserver that can serve files out of the current directory on port 8000.

Later, I may try to use ffserver or motion to do something fancier, but I’m happy with this setup so far.

The Raspberry Pi, and the WiPi USB dongle, with questions about power…

I’m having lots of fun with my Raspberry Pi, and I’ve decided to launch one of my crazy spare-time projects: inspired by this article detailing the construction of RUDEBOT, a kind of mobile tablet robot, I decided to build a robot of my own. But, anything worth doing is worth changing and adapting, so I thought I’d put my own spin on it, and use the Raspberry Pi and an Arduino combined to provide the brains of this robot.

wipi The original incarnation of the RUDEBOT used an Arduino and a WiFi shield. I didn’t like this approach because the WiFi shield is, by all accounts, fairly buggy and limited, and the actual cost of the shield is rather high. It dawned on me that I could buy a Raspberry Pi and a WiPi (the tiny USB 802.11n dongle you see on the right) for less money, and I’d have the advantage of also carrying a full Linux computer on board. I could turn my incarnation into a kind of mobile hotspot, which seemed like it might be a fun thing to try. I would still use the Arduino to handle the basic motor controls with commands sent over a serial port from the Raspberry Pi. Having that much hardware on board makes all sorts of software possible.

robotWhile waiting for parts to arrive, I just sort of began doodling using OpenSCAD to design the basic layout. I built CAD models for the motors and the platform, and selected a 12V 7 amp hour SLA battery. And then… began to think about power.

The documentation says that the Raspberry Pi can consume up to 700ma of current while running. Often, you’ll see that whenever anyone adds any significant USB peripherals to a Pi, they use a powered USB hub. In theory USB devices are supposed to be able to request 500ma of current from the host, but the Raspberry Pi (the second version, the originals ones had even lower power capabilities) has fuses which limit total current at 1A. So clearly, power hungry peripherals can require additional power, and a powered hub is the common solution.

But which peripherals are power hungry? In particular, it would be great if I could hook up the WiPi wireless dongle without having to provide additional power. So, I set out to research whether it was feasible. I saw three ways to proceed:

  1. Simply try it. If it works, then it works. Hard to argue with that, but it doesn’t tell you a lot about what the limits are. Would adding an additional keyboard/bluetooth/mouse push it over the edge? Undisciplined trial and error doesn’t seem like a good approach.
  2. Try it, but measure the current required. This seems like a better approach, and gives you hard data. Basically I’d need to tap a microusb cable so that I could put in my trusty multimeter, and I could measure the current directly. I probably will do that soon.
  3. Try to research how much current these things are spec’ed to use.

The last is easy to do when you have an iPad in front of you during lunch, so that’s of course what I did. But the figure that I’m interested in (power consumption) is not very often listed for USB peripherals. But it turns out that USB peripherals are supposed to give some hints to the host about how much power they require. If you plug in a peripheral to your Linux box (including the pi), you can simply use the command “lsusb -v” to get a dump of all the USB devices, and see what the driver for the hardware thinks it needs.

So, I did just that. I plugged my WiPi into the powered hub, and had a peek. Here’s the dump, most of which is pretty uninteresting, but which contains the Max Power…

Bus 001 Device 008: ID 148f:5370 Ralink Technology, Corp. RT5370 Wireless Adapter
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.00
  bDeviceClass            0 (Defined at Interface level)
  bDeviceSubClass         0
  bDeviceProtocol         0
  bMaxPacketSize0        64
  idVendor           0x148f Ralink Technology, Corp.
  idProduct          0x5370 RT5370 Wireless Adapter
  bcdDevice            1.01
  iManufacturer           1 Ralink
  iProduct                2 802.11 n WLAN
  iSerial                 3 1.0
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           67
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          0
    bmAttributes         0x80
      (Bus Powered)
    MaxPower              450mA

450ma. Ouch. That seems like a lot. I find it kind of hard to believe that such a tiny transmitter can pull that much current without melting itself (it’s tiny). So, I’m left with some questions:

  1. Can the WiPi really pull that much current?
  2. Can I use it without plugging it into a powered hub? Some people do seem to be doing that, and have even said that it doesn’t work properly when plugged into some powered hubs.
  3. I’m also interested in hooking an Arduino to the Pi. How much current can the Arduino pull?
  4. Ultimately, I probably will want to power all of this equipment with my 12V SLA battery. What’s a reasonable solution to powering the Pi, Arudino and potentially a powered hub? I’m looking for quick and dirty, but also reasonable and reliable.

Any hints from robot/power/Raspberry Pi experts are welcome, here or via twitter @brainwagon.

More OpenSCAD tinkering…

The other day, I was at Harbor Freight, and picked up an inexpensive set of digital calipers. While goofing around, I decided to try to reproduce an object using OpenSCAD. I had just received a pair of motors from Pololu. They look like this:

pololu

I sat in the editor, and set to work measuring. I didn’t really plan this out, I just hacked until the CSG operations worked out. I did not model the flat on the drive shaft, and only modeled the six mounting holes, not the three that hold the motor together on the front. But overall, in about half an hour, I learned a bunch and came up with the following model.

motor1

motor2

I’ll probably model the flats, and then figure out how to instantiate two of these and model a robots platform I’m imagining. More later.

Tinkering with OpenSCAD…

It seems like I might have access to a 3D printer, so I thought it might be fun to see what I could design. I thought using OpenSCAD, a scripting based 3D modelling software that can generate models in STL, a fairly simple but flexible format that can be printed by lots of printers. I had never used it before, but I am fairly comfortable with the ideas, so I thought I’d try a simple project: designing a 6″ (150mm diameter) wheel for a robotics project that I’m working on. It’s not really meant to be serious for construction (it is solid and uses way too much material) but it was a modest first attempt. Here’s the program I came up with in 20-30 minutes.

module dumbwheel() {
	difference() {
		union() {
			difference () {
				cylinder(h=25, r=75, center=true, $fn=200) ;
				cylinder(h=35, r=50, center=true, $fn=96) ;
			}
			rotate(a=90, v=[1, 0, 0]) {
				for (r = [0, 60, 120, 180, 240, 300]) {
					rotate (a=r, v=[0, 1, 0]) { cylinder(h=60, r=4, $fn=16) ; }
				}
			}
			cylinder(h=25, r=12, center=true, $fn=100) ;
		}
		cylinder(h=35, r=3, center=true, $fn=100) ;
	}
}

dumbwheel() ;

And it generated the following wheel.

simplewheel

I’ll have to think about this a bit more to generate a pragmatic wheel, but it’s not a bad start.