AquaMan
Posts: 14
Joined: Wed Dec 19, 2012 12:01 am

Using USB instead of GPIO

Sat Oct 19, 2013 5:07 am

I'm at the point where I'd like to switch from using the GPIO to using USB for control, but need a bit of guidance to get started. More specifically I'd like to control an MCP23017 - i2c 16 input/output port expander, or perhaps a ULN2803: 8 Channel Darlington Driver

Can anyone give me a bit of a push in the right direction? I'm really not sure where to even start. I've gotten as far as import serial in python, but that's about it.

I see a lot of USB controlled relays available and I'd like to be able to build my own.

Thanks.

Heater
Posts: 14468
Joined: Tue Jul 17, 2012 3:02 pm

Re: Using USB instead of GPIO

Sat Oct 19, 2013 5:50 am

Get yourself an STM32F4 Discovery board. About 20 dollars http://www.st.com/web/catalog/tools/FM1 ... 2/PF252419 Scroll down that page for picture.

It's got tons of pins you can use as GPIO, it's got PWM, UART I2C, all kinds of stuff.

It has USB connectors that you can program and then talk to it with as if over USB serial. Easy to do from your Python on the Pi.

Now, that is going to mean you have to get into programming the ARM processor on the discovery in C or C++ which is fun but might be a bit too much effort.

So, load it up with the Espruino JavaScript interpreter and then it is very easy to control all of the peripheral I/O devices with simple JavaScript commands sent down that USB/Serial link.

Espruino is a free download here: http://www.espruino.com/ Do watch the video on tat page to see how easy it is to use.

Of course on that page you will find many other similar boards that you could use, perhaps easier to get or cheaper.
Memory in C++ is a leaky abstraction .

AquaMan
Posts: 14
Joined: Wed Dec 19, 2012 12:01 am

Re: Using USB instead of GPIO

Sat Oct 19, 2013 10:55 pm

I actually already have something similar, but now that I've had a chance to think about it allow me to explain it a bit better.

On the left side of my desk I have a computer with python and usb. The right side has a small dc motor that I'd like to move forwards and back. Right now I'm stuck on how to get from one side to the other.

I have an L293 chip connected to the motor, and I can connect an I2C chip to that. But it leaves me with how to talk to the chip.

The funny thing is that I have a USB to TTL Serial Cable (http://www.adafruit.com/products/954) which I plug into my R-Pi, and then use the GPIO to talk to the chips. To me this seems like over kill. I should be able to do something that would allow me to go from USB straight through to the motor control.

Online I've found a few off the shelf products like http://www.yoctopuce.com/EN/products/us ... powerrelay but it's $25 and good for only one purpose. The closest thing to what I have in mind is http://numato.com/8-channel-usb-gpio-module which is $20.

It bothers me that I can't figure out how to make one of those myself. What is between the mini-usb plug and the GPIO pins?

TheWalkingCactus
Posts: 14
Joined: Fri Mar 29, 2013 7:53 pm

Re: Using USB instead of GPIO

Sun Oct 20, 2013 12:02 am

I've never messed around with using the wires in a USB before, but regarding your final question:
What is between the mini-usb plug and the GPIO pins?
There is a small microcontroller between that reads/replies to the serial data the USB cable is giving it, and then uses that data to turn on/off the relays. I looked at the blueprint for the Yocto-PowerRelay and found that it uses a 'PIC24FJ64GB002' chip, which is a 28-pin 64GB model.
I did a quick google and found the website where you can buy/read documentation here: http://www.microchip.com/wwwproducts/De ... e=en536121

I think that that's the kind of chip you need, just something small to read the USB serial data and do something with it.

Hope this helps!
- TheWalkingCactus

Heater
Posts: 14468
Joined: Tue Jul 17, 2012 3:02 pm

Re: Using USB instead of GPIO

Sun Oct 20, 2013 7:59 am

AquaMan,
What is between the mini-usb plug and the GPIO pins?
Be aware that the protocol used over USB is quite complicated. A lot of complex messages are going backwards and forwards over those two serial wires in USB.

There needs to be some thing "intelligent" at the receiving end to understand all those messages. Perhaps a PIC micro-controller as suggested or perhaps a specially made USB controller chip like the FT245R http://www.ftdichip.com/Products/ICs/FT245R.htm

These USB parallel dongles are often not so cheap hence my suggestion to try a little board that runs Espruino, then you have your USB to parallel, but also USB to PWM for motor control, or USB to serial and so on. Plus playing with JavaScript is on those little boards is a lot of fun. All of a sudden your USB to parallel device can also be even more intelligent than just a USB dongle and a lot more flexible in what you can use it for.
Memory in C++ is a leaky abstraction .

User avatar
joan
Posts: 14695
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Using USB instead of GPIO

Sun Oct 20, 2013 8:06 am

AquaMan wrote:...
I have an L293 chip connected to the motor, and I can connect an I2C chip to that. But it leaves me with how to talk to the chip.
...
The Pi supports I2C "out of the box". There are Python modules such as smbus.

gordon77
Posts: 4517
Joined: Sun Aug 05, 2012 3:12 pm

Re: Using USB instead of GPIO

Sun Oct 20, 2013 10:34 am

Maybe not what you want but I use an Arduino UNo to drive motors as they do PWM (I know the RPi can also do PWM)

The circuit below drives the focus motor on my telescope either by the rotary pot or can accept ASCII commands over the USB from a PC/RPi etc. I can let you have software for the Uno if you want.

Gordon77
Attachments
Uno Focus.jpg
Uno Focus.jpg (34.22 KiB) Viewed 4801 times

neilf
Posts: 72
Joined: Sun Nov 11, 2012 8:14 am

Re: Using USB instead of GPIO

Sun Oct 20, 2013 10:37 am

Goodness me, you folks don't half like things complicated!

The L293 should work fine connected directly to the GPIO pins - why bother with I2C, USB, drivers, libraries, etc, etc?
I would ask has the OP actually tried driving it the simple way?

There's every likelihood the chip will respond to the Pi's 3.3v outputs and switch its output on - why not try it and see first? You have to hold the 'Enable' pin high to enable the relevant channel (connect the pin to the Pi's 5v pin) and then just toggle two GPIO pins high and low to control the motor.
GPIOx = high and GPIOy = low drives the motor one way. GPIOx = low and GPIOy = high drives the motor the other way. Set both pins high or both low to stop the motor. You can also use the enable pin to start and stop the motor via a press switch if you like - just make it switch between 5v and 0v and the motor should start and stop - your GPIO pin settings will decide which way the motor rotates.
Here's a short simple explanation of the chip: http://www.me.umn.edu/courses/me2011/ar ... /L293.html along with a 'truth table' of the inputs and outputs. A lot of 5v TTL logic inputs will still drive OK with the Pi's 3.3v outputs, so it's always worth trying that way first.

gordon77
Posts: 4517
Joined: Sun Aug 05, 2012 3:12 pm

Re: Using USB instead of GPIO

Sun Oct 20, 2013 2:43 pm

If you can replace the 2 uno outputs in my drawing , into the 293 , with 2 gpio outputs from the RPi, and make one of them pwm it should work just the same.

TheWalkingCactus
Posts: 14
Joined: Fri Mar 29, 2013 7:53 pm

Re: Using USB instead of GPIO

Sun Oct 20, 2013 3:05 pm

Guys, I'm pretty sure the OP doesn't care about other, simpler ways to drive the motors, he/she wants to learn about using a USB connection for physical computing. Hence the title being 'USB instead of GPIO'

gordon77
Posts: 4517
Joined: Sun Aug 05, 2012 3:12 pm

Re: Using USB instead of GPIO

Sun Oct 20, 2013 3:09 pm

TheWalkingCactus wrote:Guys, I'm pretty sure the OP doesn't care about other, simpler ways to drive the motors, he/she wants to learn about using a USB connection for physical computing. Hence the title being 'USB instead of GPIO'
That's why I showed him my Uno option that uses USB :D

TheWalkingCactus
Posts: 14
Joined: Fri Mar 29, 2013 7:53 pm

Re: Using USB instead of GPIO

Sun Oct 20, 2013 3:14 pm

gordon77 wrote:
TheWalkingCactus wrote:Guys, I'm pretty sure the OP doesn't care about other, simpler ways to drive the motors, he/she wants to learn about using a USB connection for physical computing. Hence the title being 'USB instead of GPIO'
That's why I showed him my Uno option that uses USB :D
Yeah, I was mainly talking about some of the other people in this thread ;)

*Cough* Neilf & Joan *Cough*

AquaMan
Posts: 14
Joined: Wed Dec 19, 2012 12:01 am

Re: Using USB instead of GPIO

Sun Oct 20, 2013 8:55 pm

"I would ask has the OP actually tried driving it the simple way? "

Yes. But when looking at my setup, it occurred to me that the RPi/GPIO represented $35 in between my USB and the motor (or anything physical). I found it odd that I needed something so [relatively] expensive and complicated for what seemed like a simple task.

When looking at alternatives, I was some what shocked that any other off the shelf product was still going to be at least $20. And digging a bit deeper I kept seeing Heater's comment, "Be aware that the protocol used over USB is quite complicated. A lot of complex messages are going backwards and forwards over those two serial wires in USB."

The answer to half of my question ended up being "PIC processor" which I can get for $1 then try to write my own firmware, or buy a pre-programed one for USB for $5. If I get it on a board with the USB socket, and output pins, it's $22 at Sparkfun
https://www.sparkfun.com/products/199 Or something similar
here http://electronics-diy.com/store.php?se ... B_IO_Board

Which makes the RPi and Arduino variations seem cheap in comparison.

TheWalkingCactus
Posts: 14
Joined: Fri Mar 29, 2013 7:53 pm

Re: Using USB instead of GPIO

Thu Oct 24, 2013 2:09 am

AquaMan wrote:"The answer to half of my question ended up being "PIC processor" which I can get for $1 then try to write my own firmware, or buy a pre-programed one for USB for $5.
So you're all set? Got all the help you need? (For now at least :) )

Return to “General discussion”