Question about using raspberry pi to drive two servos, laser

10 posts
by wolftiger » Thu Jun 13, 2013 9:55 am
I'm building a laser toy for the cat using two servos and a laser pointer. This video actually shows a similar project, but this guy is driving the control of his with a watch:

Anyways, this tutorial on a facetracking webcam will help me set up the servos:

But my question is how to connect the laser pointer to the raspberry pi. Do I just plug it straight into the gpio? What pins?

I'm planning on using servoblaster (viewtopic.php?f=37&t=15011) to control the servos and would like the ability to turn the laser on and off through the pi. This is the laser I purchased:

I'm a bit of a newb on robotics. I'm afraid to fry the laser or the pi just by plugging things in. Any help you can offer would be appreciated.

Posts: 2
Joined: Thu Jun 13, 2013 9:26 am
by MangoKid » Thu Jun 13, 2013 1:36 pm
I hate to say it but its never that easy. You cant plug the laser into the GPIO for many reasons, the most important being that the voltage levels are probably different.

Your best bet would be to use a basic relay module. Something like this

A relay is a switch that is powered by a lower voltage/lower current source, in this case the Pi. When the pi GPIO turns on or off the relay will do the same. Wire the ground of the battery/power supply to the laser. The take the power (probably the red wire) and wire both (laser and power supply) into either side of the relay.

This way your Pi is safe! :)
Posts: 51
Joined: Tue Apr 02, 2013 12:24 pm
by toxibunny » Thu Jun 13, 2013 2:00 pm
Yep, relay would be easiest.
note: I may or may not know what I'm talking about...
Posts: 1383
Joined: Thu Aug 18, 2011 9:21 pm
by joan » Thu Jun 13, 2013 2:13 pm
You can only power a small hobby servo through the Pi (circa 150 mA).

You can control as many as you want (21 directly with the rev. 2 Pi).
User avatar
Posts: 12654
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by joan » Thu Jun 13, 2013 2:19 pm
There is a laser pointer in this video.
Last edited by joan on Fri Dec 20, 2013 9:28 am, edited 1 time in total.
User avatar
Posts: 12654
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by PiGraham » Thu Jun 13, 2013 2:46 pm
According to the specifications for your laser module:
2.8 - 5.2V DC voltage input
25mA max current draw

You can run it from 3.3V, but any one GPIO pin can supply a max of 16mA (see here), so you could not drive the laser at full power from a GPIO. If your servo control outputs don't draw too much power you could drive the laser at reduced power (use an appropriate series resistor to limit the current to < 16mA).

It is best to drive the laser with a transistor. No need for a relay, which will take at least as much power to energise the coil as you need for the laser. Something like these will do for this and many other power switching tasks. You could also use a MOSFET.

For the NPN darlington connect the base to the GPIO output through a resistor (100R to 10K will do), connect the cathode (-ve) of the laser to the collector and the emitter to 0V. Connect the anode (+ve) of the laser to 3.3V or 5V supply via a resistor. When the GPIO is high the transistor will turn on and allow current to flow through the laser.

It isn't clear on Adafruit is the module has inbuilt current limiter. They say "It's a diode with integrated driver". You may not need a series resistor if your power circuit can supply 25mA, but go carefully or you may blow the diode.

Check if the module has a current limiter in it.

You could cheat and use two GPIO outputs commoned via two diodes. DO NOT connect GPIO pins together directly.
Posts: 2478
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville
by mikerr » Thu Jun 13, 2013 3:09 pm
joan wrote:You can only power a small hobby servo through the Pi (circa 150 mA).

You can control as many as you want (21 directly with the rev. 2 Pi)

21 ? I thought servoblaster was only 8 ?
Is 21 with your pigpio lib ?

My biped is using up all eight and needs more....
Android app - Raspi Card Imager - download and image SD cards - No PC required !
User avatar
Posts: 2403
Joined: Thu Jan 12, 2012 12:46 pm
Location: Up north , UK
by MangoKid » Thu Jun 13, 2013 3:18 pm
Are you wiring multiple servos on 1 channel? i know some people have done this for hexa pods, because some legs make the same motions.

I dont think it works for quadrapeds though...
Posts: 51
Joined: Tue Apr 02, 2013 12:24 pm
by wolftiger » Thu Jun 13, 2013 5:03 pm
Wow. Thank you all for the help!
Posts: 2
Joined: Thu Jun 13, 2013 9:26 am
by raser » Thu Feb 25, 2016 4:30 pm
I am Po powering a Laser Diode from my Pi. It is a cheapy laser pen that runs on about 4.0 V normally in little batteries. My only problem is the correct resistance - it's been trial and error and my kit only came with 10k ohm and 330 ohm.

(This is just a very basic method - how long the diode will last like this is another question. Now I know I can power a small laser, I plan on constructing a better laser driver and powering a DVD RW burning laser - it will need a decent focusing module £20 / £30)

I have the GPIO pins connected with a ribbon type cable to a small prototype style circuit board (it was a Vilros kit)

To Test it, I tried it with direct + and - voltage (not the input / output switchable pins on the board, the actual voltage outputs) and got an LED connected up with a resistor and jumper wires (the most basic LED + resistor circuit) I have my laser pen stripped, red and black + and - wires soldered to the tiny board on the diode circuit board (I'm sure it has some sort of voltage control and heat sink like chip - not bad for a £2 laser pen!) The stripped and wired diode, board, and focusing module (very basic lens) is all mounted inside an old small cctv housing - looks the business like one of them expensive laser modules.

I just have the jumper wires from my Laser unit going straight to the Pi's prototype board. It will power fine off the 3V with NO resistors but would probably burn itself out - lasers are greedy. So I now have it on the 5V power source with a 330 ohm resistor - it works but is a little dim and only close range. I need less resistance (lower value resistor) to get a more powerful beam.

I have pics of the laser module and Pi setup if needed just let me know. If the jumper wires are then moved to GPIO pins of the same voltage a Python script can be written to turn it on and off, blink, detect a switch to turn it off and on like a safety feature - all the same things you can do with a LED.

I DONT RECCOMEND dismantling an expensive laser pen or diode assembly to do this rough and ready method as you will be moaning e=when the laser burns itself out - as I mentioned earlier - A Laser Driver Circuit for the colour of laser diode you have is needed for safe and efficient use for many many hours. I did a project yonks ago with the DVD RW and built a laser driver, I still have the guide and wiring diagrams - though there are Tons of them online, oyu can even buy a component kit - with the laser diode to build yourself. This method is FINE for a cheapo laser pen that is of no great loss. Mine has lasted AGES! before running it like this I had it on a low voltage 4V DC phone charger type adapter - it seems to have built-in proptection!)

When I build a Pi robot someday it would be cool to put a programmable balloon popping burning laser on it!

Posts: 65
Joined: Mon Jan 25, 2016 1:49 pm