Serial communication from Linux to arduino nano ok. mega 2560 not activating.

Hi

I am trying to send serial commands to my arduino mega2560 connected to the usb port. but having a few problems. If I send commands to my mega board in the arduino ide it all works fine. but I would like to send commands in the background without the ide running.

I have found this very helpful website http://www.stealthcopter.com/blog/2010/02/python-interfacing-with-an-arduino/.

that describes how to set everything up in python.

I have got it working fine from my computer to my arduino nano board on the usb. using the sketch on the web page.

but if I run the same code on my mega board, I can see the tx rx lights respond. but the led pin is not triggered.

I have made sure the python script is communication on the correct serial channel. But am unable to get the mega board to change the state of the led pins.

Does anyone know why there is a difference between the communication with the nano board and the mega board. Has anyone had this problem and know of a solution.

Thanks

Simon

If I send commands to my mega board in the arduino ide it all works fine.

The Arduino IDE can not send commands to the Mega. The Serial Monitor application launched by the IDE can be used to send text to the Arduino. That the Arduino interprets the text as commands is irrelevant.

but I would like to send commands in the background without the ide running.

What does this mean? What is going to be sending the text?

There is a bunch of Arduino code on that site. There is a bunch of Python code. Don't expect us to guess which you are using.

Are you running the same sketch on the Mega as on the Nano (or as nearly the same as it can be)? If not, try that.

Is the Mega using the same Comm port as the Nano, or, if it is different, is the correct port in the Python code?

...R

Hi

Thanks for the response.

I am using exactly the same sketch on both the nano and the mega boards. as it is on the web site. I am using exactly the same python script for both boards. as it is on the web site. the only thing that changes is the usb port the nano board connects as /dev/ttyUSB0 where as the mega board connects as /dev/ttyACM0. but I have the python script set up to work on both ports. and the python script says the board is connected ok.

the nano is working correctly. The mega tx and rx leds flash but no response from the board.

If I use the serial monitor in the arduino ide both boards work fine as they should do.

when I said I want to send commands in the back ground. I am running the computer headless so there is no desktop running. so I am wanting to communicate with the mega board from the command line. via ssh bash or python.

I am using exactly the same sketch on both the nano and the mega boards. as it is on the web site.

But you are not going to say what that code is.

I am using exactly the same python script for both boards. as it is on the web site.

But you are not going to say what that code is.

Therefore, all we can do is wish you luck. Good luck.

I agree with @PaulS. There is too much junk on that web page for us to know what exactly you are talking about. If you want help make life easy for us by posting the actual code you are using here (in code tags, please). And I hope the code is short.

...R

Hi

sorry I didn't add the sketch earlier. I replied from my phone. and was unable to add the sketch.

Here is the sketch I'm using

int ledPin = 13;  // choose the pin for the LED
int val = 0;      // variable for reading the pin status
char msg = '  ';   // variable to hold data from serial


void setup() {
  pinMode(ledPin = 13, OUTPUT);      // declare LED as output
  Serial.begin(9600);
  Serial.print("Program Initiated\n");  
}

void loop(){
        // While data is sent over serial assign it to the msg
    while (Serial.available()>0){ 
        msg=Serial.read();
    }

  // Turn LED on/off if we recieve 'Y'/'N' over serial 
  if (msg=='A') {            
    digitalWrite(ledPin = 13, HIGH);  // turn LED ON
    Serial.print("LED Activated\n");
    msg=' ';
  } 

  // Turn LED on/off if we recieve 'Y'/'N' over serial 
  if (msg=='a') {            
    digitalWrite(ledPin = 13, LOW);  // turn LED OFF
    Serial.print("LED Deactivated\n");
    msg=' ';
  } 

}

and the python script

import serial  
import time  
  
locations=['/dev/ttyACM0','/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ttyUSB3','/dev/ttyS0','/dev/ttyS1','/dev/ttyS2','/dev/ttyS3']
    
for device in locations:    
    try:    
        print "Trying...",device  
        arduino = serial.Serial(device, 9600)   
        break  
    except:    
        print "Failed to connect on",device     
  
try:    
    arduino.write('A')    
    time.sleep(1)  
    print arduino.readline()  
except:    
    print "Failed to send!"

Your comments in the sketch suggest the Python is sending Y/N but the code and the Python script seem to use A/a - just adds to the confusion.

Your Python script seems only to send an 'A' and never an 'a' (so the LED won't go off)

Can you modify the sketch so that receiving an 'A' causes the led to blink twice and receiving an 'a' causes it to blink 3 times. That will allow you to see if the Mega is receiving but not transmitting.

...R

  pinMode(ledPin = 13, OUTPUT);      // declare LED as output

ledPin already had a value (13). Why are you assigning a new one?

        // While data is sent over serial assign it to the msg
    while (Serial.available()>0){ 
        msg=Serial.read();
    }

Throw away all but the last character that has arrived. Why?

    digitalWrite(ledPin = 13, HIGH);  // turn LED ON

ledPin already had a value (13). Why are you assigning a new one?

    digitalWrite(ledPin = 13, LOW);  // turn LED OFF

ledPin already had a value (13). Why are you assigning a new one?

The python script generates some output. That output is important. Where is it?

Hi

Thanks for the help.

I have made some adjustments to the sketch and removed the 13 from the digital write as you said. I didn't know quite what to change in the sketch from your comment "Throw away all but the last character that has arrived. Why?"

And you said the python script is missing some code for the output. I don't know anything about python this is just from that website. But this is the response I get in the terminal when trying the mega board.

Trying... /dev/ttyACM0 PProgram Initiated

the nano board is still working as it should, but the mega board is still not. all it does is flash the tx and rx led and the led 13 flashes briefly. even if I put in a 3 second delay in the sketch the led 13 just briefly flashes.

I don't understand why the nano works fine where as the mega doesn't. yet when sending the commands in the arduino ide serial monitor both boards work fine.

here is a copy of the sketch now

int ledPin = 13;  // choose the pin for the LED
int val = 0;      // variable for reading the pin status
char msg = '  ';   // variable to hold data from serial


void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  Serial.begin(9600);
  Serial.print("Program Initiated\n");  
}

void loop(){
        // While data is sent over serial assign it to the msg
    while (Serial.available()>0)
{ 
        msg=Serial.read();
    }

  // Turn LED on/off if we recieve 'Y'/'N' over serial 
  if (msg=='Y') {            
    digitalWrite(ledPin, HIGH);  // turn LED ON
    Serial.print("LED Activated\n");
    msg=' ';
  } 

  // Turn LED on/off if we recieve 'Y'/'N' over serial 
  if (msg=='N') {            
    digitalWrite(ledPin, LOW);  // turn LED OFF
    Serial.print("LED Deactivated\n");
    msg=' ';
  } 

}

I didn't know quite what to change in the sketch from your comment "Throw away all but the last character that has arrived. Why?"

    while (Serial.available()>0)
{ 
        msg=Serial.read();
    }

If there is more than one character available to read, only the last one will be in msg when the while loop ends. If you want to deal with every character, get rid of the while loop. If you don't want to deal with every character, don't send the ones you don't want to deal with.

And you said the python script is missing some code for the output.

No. I said it is generating some output. There are statements like this:

        print "Trying...",device

Where is that data?

Hi

the output is displayed in the linux terminal. this text "Trying... /dev/ttyACM0 PProgram Initiated" is comming from the arduino sketch.

I have changed the void loop while to if in the sketch.

void loop(){
        // While data is sent over serial assign it to the msg
    if (Serial.available()>0)
{ 
        msg=Serial.read();
    }

and again the nano board works fine. and I have also tried the sketch on a Duemilanove 328 board and the sketch works fine.

But I am still having no luck with the mega board. Its still doing the same thing flashing the tx lights then briefly flashing the led pin.

I am thinking it's a hardware difference. does anyone know of what the hardware difference is and if there is a software solution. I don't know what the difference is with the communication in the arduino ide serial monitor and using the python script. in the way that it works. could it be anything to do with the baud rates.

Hi

Just wanted to let everyone know. I have solved the problem.

It was a hardware problem.

I had to disable the auto reset function. After reading on line I found there are some gold solder pads on the back of the arduino mega2560.

one pair next to the usb have a small link wire. These are used to connect the usb. these need to be linked for the usb to work.

The other two are next to the 6 pin jumpers. to get it working I had to upload the sketch to the board. then solder a link between the two gold pads closest to the jumper links.

when the shorting link is in place the board communicates properly.

To upload a different sketch the link will need removing.

Are you using a genuine Arduino Mega2560?

Mine is exactly like the photo on the product page and has 4 silver coloured solder tags with nothing connected to any of them.

On the component side of my board there is a small link marked RESET-EN which is connected. You would disconnect it to disable reset.

...R

Hi

You must have a different version board to me.

my board has 4 gold solder pads on the back. but no link on top as far as I saw.

Well, I know I have the standard board.

What board have you got?

...R