Can an Arduino be coded to run a sketch when plugged into any USB port?

Just to expand on the subject line the sketch is to send an RF signal to an RF controlled relay.

I have used the code on a MEGA 2560, Leonardo and Pro Mini. Currently the code switches off the relay but I guess I could set a pin high for the code to decide if it should turn the relay off or on.

What I would like the Arduino to do is to be able to plug into a USB port on any device (Windows Laptop, Raspberry Pi, Linux server etc) and have the code run.

My main code for the host device would probably be written in Python. Is there any way of setting a pin high on an Arduino from the host machine so it could decide if the relay should be turned on or off (or to wait for the next on / off signal)?

I guess in Python I can create a function to check which operating system the host is running and then run different pieces of code. I don't know how to interface between the host and the Arduino to set a pin high or some other 'interrupt' for the Arduino to send the RF signal (on, off or wait).

Is this possible? If it is could you indicate the name of the routine / library etc required for Windows and Linux on the host machine.

I would prefer to use the cheapest Arduino possible but I did have 'power' problems with the 5V Pro Mini when I was testing it yesterday. I took a look at how the Pro Mini can be powered but the only way that worked was through the 'FTDI' i.e. vcc and gnd connected to my Mega. If I connected external power directly to RAW or VCC without the Mega it wouldn't send the RF signal (from RF transmitter on pin 2). I only provided 5V to RAW and the transmitter needs 3.3V to 5V but I was worried I might blow the Pro Mini if I gave it too much power. It appears RAW can take up to 12V. Will it be safe to provide 12V or is there some thing I am missing about powering the Pro Mini?

An arduino will run what ever code it has running on it when it is powered up. This includes being powered by USB.

Connecting USB to say an Uno or via a USB to serial converter to a Pro mini makes a serial port on the computer and that can talk to the serial of the Arduino.

You can write scripts in Python to talk to serial. Your sketch can listen to and talk over serial.

http://arduino.cc/en/Main/ArduinoBoardProMini

The Arduino Pro Mini can be powered with an FTDI cable or breakout board connected to its six pin header, or with a regulated 3.3V or 5V supply (depending on the model) on the Vcc pin. There is a voltage regulator on board so it can accept voltage up to 12VDC. If you're supplying unregulated power to the board, be sure to connect to the "RAW" pin on not VCC.

You have either a 16 MHz or 8 MHz board. You can feed it up to 12V through the regulator on the RAW pin but understand that the regulator may get real warm. IF -- you have a regulated power supply of the right voltage THEN you can connect to VCC. IF -- you have added devices that use much power THEN you should supply that externally and all grounds should be connected. IF -- you connect 3.3V device to a 5V device THEN you need extra circuits for voltage leveling.

Easy guess for me is that any ProMini can take regulated 5V on VCC and run but only an 8 MHz ProMini can run on 3.3V.

pieman: What I would like the Arduino to do is to be able to plug into a USB port on any device (Windows Laptop, Raspberry Pi, Linux server etc) and have the code run.

My main code for the host device would probably be written in Python. Is there any way of setting a pin high on an Arduino from the host machine so it could decide if the relay should be turned on or off (or to wait for the next on / off signal)?

I guess in Python I can create a function to check which operating system the host is running and then run different pieces of code. I don't know how to interface between the host and the Arduino to set a pin high or some other 'interrupt' for the Arduino to send the RF signal (on, off or wait).

This is very confusing. First para .... if there is a program loaded in an Arduino it will run as soon as the Arduino is powered up. The Arduino won't care where the power comes from.

Second para .... this sounds like you need communication between the PC and the Arduino. Is that communication going to operate over the USB connection? If so what is the point of the first Para?

Third para .... why would it matter what operating system is on the host? Do you want the Arduino to do different things for different PCs?

As far as I know Python code will run on Linux, Windows and Mac.

This demo may have some useful stuff.

...R

I don' t think the arduino will run Python scripts. Have you tested that idea by running one ? I haven't seen any posts about doing that.

I haven't seen any discussion of a Python compiler for AVR processors. Python is an interpreted language (vs compiled), closest similar would be Bitlash for Arduino. www.bitlash.net

Maybe that BASCOM language that ELEKTOR magazine uses too. Never tried either myself. http://www.mcselec.com/?option=com_content&task=view&id=14&Itemid=41

The other language that comes up from time to time is FORTH, discussed in a Playground article http://playground.arduino.cc/CommonTopics/ForthOnArduino

GoForSmoke: http://arduino.cc/en/Main/ArduinoBoardProMini

You have either a 16 MHz or 8 MHz board. You can feed it up to 12V through the regulator on the RAW pin but understand that the regulator may get real warm. IF -- you have a regulated power supply of the right voltage THEN you can connect to VCC. IF -- you have added devices that use much power THEN you should supply that externally and all grounds should be connected. IF -- you connect 3.3V device to a 5V device THEN you need extra circuits for voltage leveling.

Easy guess for me is that any ProMini can take regulated 5V on VCC and run but only an 8 MHz ProMini can run on 3.3V.

Earlier today I fed over 10V from a laptop battery to RAW on the the Pro Mini and it will not activate the RF transmitter I have on pin 2. The power for the transmitter is actually from a Leonardo so it is just the data I am try to send from the Pro Mini. If I power the Pro Mini from RAW or VCC from my Mega 2560 pin 2 sends the RF signal (from the sketch running on the Pro Mini). Strange? It is the 5V 16MHZ unit I have.

Robin2: This is very confusing. First para .... if there is a program loaded in an Arduino it will run as soon as the Arduino is powered up. The Arduino won't care where the power comes from.

Second para .... this sounds like you need communication between the PC and the Arduino. Is that communication going to operate over the USB connection? If so what is the point of the first Para?

Third para .... why would it matter what operating system is on the host? Do you want the Arduino to do different things for different PCs?

As far as I know Python code will run on Linux, Windows and Mac.

This demo may have some useful stuff.

...R

I am aware the Arduino's run the code when they are powered up. Yes I will need to communicate between host and Arduino and presumably via the USB port. I believe different OS interact differently with the USB port as far as communicating with the Arduino. Thanks for the link to the comms demo. It looks like it will move me forward.

pieman: Earlier today I fed over 10V from a laptop battery to RAW on the the Pro Mini and it will not activate the RF transmitter I have on pin 2. The power for the transmitter is actually from a Leonardo so it is just the data I am try to send from the Pro Mini. If I power the Pro Mini from RAW or VCC from my Mega 2560 pin 2 sends the RF signal (from the sketch running on the Pro Mini). Strange? It is the 5V 16MHZ unit I have.

Do you have all the grounds connected?

How many mA will that 10V output deliver? Measure it through a resistor, like a 10k resistor. Do not just measure volts or amps directly, your readings will be off.

Shop around for power. I bought 5V 1A wall plug switching power supplies from Yourduino for $2.50 each, but I don't know if they are regulated. Maybe Terry King (his store) could tell. You can get power plugs from lots of places. They have 9V, 12V and 3.4V wall warts as well, usually 1 Amp or more.

I am interested in that ProMini regulator and how it works to take 5V in and give 5V out.

GoForSmoke: You can get power plugs from lots of places.

You sure can. Some of them may even be safe enough to plug into a power point.

pieman: I believe different OS interact differently with the USB port as far as communicating with the Arduino.

To the extent that this is true, it won't be noticeable to the Arduino.

I'm not familiar with Windows - it may need drivers drivers to be installed. That could be an obstacle, or an inconvenience, if you want to "give" your project to a Windows user who does not know how to install drivers.

No drivers are needed with Linux (or they are already included in the OS). However, on Ubuntu at least, the user must be a member of the "dialout" group to be allowed access to the serial ports.

I don't know about MAC - but it is just Linux in a party frock.

I don't know about Android, which is also a version of Linux, I don't think there is a standard Python implementation. Its Java is not the standard JVM but there is a conversion utility.

...R

Thanks Robin2.

Looking at your demo serial interface I think all I would need to do in Python is define the serPort string differently for each OS. So "com16" for my Leonardo on Windows 8.1 and "/dev/USB0" on a Pi running Debian.

Does this sound about right?

Robin2,

I am running through your serial comms demo on Windows (after installing pyserial for the serial module).
I have set the serial port as COM16.
If I send <LED1,200,0.5> from the Arduino IDE Serial Monitor I get back:

<Msg LED1 NewFlash 200 SrvFrac 0.50 SrvPos 90 Time 832> and the LED responds accordingly (success).

But in Python it just sits at ‘Serial port COM16 opened Baudrate 9600’.
The code doesn’t seem to be resetting the Arduino (Leonardo) and therefore not receiving the “Arduino is ready” prompt from the Arduino. Any idea why the Arduino is not responding?

p.s. is this forum the most complex in the world as far as setting up email notifications for forum posts? Spent some time trying but no joy.

What you say in Reply #11 seems correct.

pieman: The code doesn't seem to be resetting the Arduino (Leonardo) and therefore not receiving the "Arduino is ready" prompt from the Arduino. Any idea why the Arduino is not responding?

I suspect this is a Leonardo issue that you would not see with an Uno or Mega.

The way to reset a Leonardo is to open the serial port at 1200 baud and then close it again. Then use your chosen baud rate. This is probably on the Leonardo product page.

...R

Robin2: What you say in Reply #11 seems correct.

I suspect this is a Leonardo issue that you would not see with an Uno or Mega.

The way to reset a Leonardo is to open the serial port at 1200 baud and then close it again. Then use your chosen baud rate. This is probably on the Leonardo product page.

...R

Same problem with the Mega, just sat waiting for the Arduino.

You should be able to get a Leonardo to work as an HID. Then anything that can run HID should see it.

GoForSmoke: You should be able to get a Leonardo to work as an HID. Then anything that can run HID should see it.

I will take a look at HID, will the Mega 2560 work as an HID too?

I now have my code running on the Leonardo with Windows (COMx) and the Pi (/dev/ACM0) by using the very basic serial interface at http://petrimaki.com/2013/04/28/reading-arduino-serial-ports-in-windows-7/

However when I run the exact same code on my Mega 2560 I get different results.

When I reset the Mega 2560 it sends Pin 12 high to light an LED and sends RF signal ON via pin 2. This is fine and duplicates what the Leonardo does.

With Python running in Windows I should be able to enter 0 to send RF signal OFF from the Arduino or 1 for RF signal back ON. The LED on Pin 12 should go on and off accordingly. On the Leonardo this works perfectly but for some reason the Mega 2560 is not responding. No RF sent and no change to the LED.

I'm pretty sure I have the pins wired up ok (pin 2 and 12 on both Leonardo and Mega) and this is borne out by the reset lighting the LED and sending the RF signal ON.

Why would the Mega perform differently to the Leonardo?

It would be much harder to get a non-USB chip Arduino to be HID than a USB chip Arduino. I think you would actually have to modify a Mega or Uno to be HID as in cut traces and solder.

And I'm not so sure that the PC can send data to a HID because I never tried.

GoForSmoke: It would be much harder to get a non-USB chip Arduino to be HID than a USB chip Arduino. I think you would actually have to modify a Mega or Uno to be HID as in cut traces and solder.

And I'm not so sure that the PC can send data to a HID because I never tried.

So the Leonardo's are quite different to the Mega's despite their 'similar' appearance? For the actual project I am looking at the Nano V3.0 COMPLETE with headers as my soldering skills are very poor. Would you expect the Nano to perform like the Leonardo (i.e. to work) or like the Mega (i.e. not work)?

pieman: Same problem with the Mega, just sat waiting for the Arduino.

Well then there is a simple problem to be fixed.

Have you my demo code on your Mega as well as the Python code on the Windows PC?

When you start the Python program is there no sign of the Mega resetting?

...R