Digital Pin changes state when Serial Communicating

I’ve got an arduino Uno connected to my Raspberry Pi via USB.

The Raspi python program requests the values of the Uno analogue pins via serial and the following sketch sends the information back. This works GREAT!

My problem is that I set the Digital Pin #8 to a LOW state, and it stays Low UNTIL the Uno is transmitting the serial information.
When I see the TX & RX LED’s flashing, Pin 8 changes from it’s Low state
(I have an LED connected to it, and the LED is lit when pin 8 is Low).
When the Serial Communication takes place, the LED goes dark, and then goes light again.).

How do I maintain a LOW state on Pin 8 during the serial communication between the Raspberry Pi and the Uno?

Thanks,
Robin

const int analogInTable[6]= {A0,A1,A2,A3,A4,A5};

char inChar;

void sendAnalogValue(byte Channel)
{
  int valueAD;
  float VoltageA0;
  valueAD = analogRead(analogInTable[Channel]);
  VoltageA0 = valueAD * (5.0/1023.0)+9.52;
  Serial.println(VoltageA0); 
}

void ReadSerial()
{
 inChar = Serial.read(); // read character
 if((inChar>='0') && (inChar <='5'))
   sendAnalogValue(inChar - '0');
 return;
}

void setup() {
  Serial.begin(9600);
  pinMode(13,OUTPUT);
  pinMode(8,OUTPUT);
  digitalWrite(8,LOW);
}

void loop() {  
  digitalWrite(8,LOW);
  delay(500);
  int sensorValue = analogRead(A0);
  if(sensorValue > 250)
    {
    digitalWrite(13,HIGH);
    }
  else
    {
    digitalWrite(13,LOW);
    }
  if(Serial.available() > 0)
    {
    ReadSerial();
    }
}
void ReadSerial()

That is NOT all that that function does.

What is connected to pin 8? Why do you care that it stays in some state? I can't see anything in the code that makes pin 8 go HIGH, so I have to question how you know that it does.

PaulS: What is connected to pin 8?

RobinGreig: (I have an LED connected to it, and the LED is lit when pin 8 is Low). When the Serial Communication takes place, the LED goes dark, and then goes light again.).

int valueAD;
VoltageA0 = valueAD * (5.0/1023.0)+9.52;

To me these 2 lines of code are inconsistent because of the data types involved.

In addition, what is the result of (5.0 / 1023.0) ?

PaulS: void ReadSerial()

That is NOT all that that function does.

What is connected to pin 8? Why do you care that it stays in some state? I can't see anything in the code that makes pin 8 go HIGH, so I have to question how you know that it does.

Currently I have an LED connected to Pin 8, however, the plan is to have it control a circuit which would turn on a battery charger depending upon the voltage read on A0. Right now the onboard LED (pin 13) is on when the voltage is above a preset level (sensorValue > 250), and off when it falls below. However the digital pins (8 & 13, that I have tried so far) change state when the Serial Communication takes place and that would cycle the Battery Charger on & off. My goal is to have the charger turned on, have the voltage monitored, and then turn off when the voltage is high enough. Right now the battery charger power cycles every time the Raspberry Pi requests a voltage reading on pin A0 of the Uno. Thanks for your help, Robin

UKHeliBob: int valueAD;

VoltageA0 = valueAD * (5.0/1023.0)+9.52;

To me these 2 lines of code are inconsistent because of the data types involved.

In addition, what is the result of (5.0 / 1023.0) ?

Ah, thanks for catching that inconsistency with the data type.

The formula is used to measure a 12V automotive battery which normally measures 13.4VDC, and when charging it rises to ~ 14.4VDC. So the battery voltage can range from 11VDC - 15VDC and the arduino can only accept 0-5VDC on the analogue input.

I use a Zener diode to reduce the battery voltage down to a level that is safe for the Arcuino.

However, each zener diode has a small difference in the actual voltage dropped, and in my case the voltage drop is 9.52VDC

So I place a 10V Zener diode between the battery and the arduino pin to drop the voltage down by 10V Battery 13.4VDC - Zener 9.52 = 3.88VDC which is a safe value to apply to the analogue pin.

The analogue pin is giving me 1024 bits of resolution over the 5VDC range, so I use that ratio * the Analogue Reading + 9.52 to give me the actual voltage of the battery. This acutally works very well to monitor the voltage of the battery.

However, if the battery charger is on, evey time the Raspberry Pi requests this information from the Arduino, the charger power cycles becasue the pin 8 changes state during the Serial Communication. Thanks! Robin

vey time the Raspberry Pi requests this information from the Arduino, the charger power cycles becasue the pin 8 changes state during the Serial Communication.

Are you sure that the Arduino is not resetting due to the Pi opening the serial connection ? Print a message in setup(). How many times do you see it and when ?

Have a look at this Python - Arduino demo. It is designed so it only opens the Serial connection once and then checks that the Arduino has finished its reset before doing anything else.

...R

Robin2: Have a look at this Python - Arduino demo. It is designed so it only opens the Serial connection once and then checks that the Arduino has finished its reset before doing anything else.

...R

UKHeliBob & Robin2, I beleive you've identified my problem! When the serial communication takes place, the Uno resets and that is why my Digital Pins change state!

Robin2, I read further down in the sample demo link you sent and you had made a comment to another user:

" I think there is an option to disable the auto-reset on the Uno board, but that would make uploading programs difficult.

I have a standalone Atmega328 on a perf-board circuit running 24/7. I can connect it to my PC with a USB-TTL cable. I have two connectors on the perf-board one of which has no connection to the reset line. If I just want to capture data from the running program I can connect the PC to that connector without causing a reset."

I simply want to poll Analogue voltages from the Uno back to my Raspberry Pi, and am using the USB-USB Cable as my serial communication.

As you mentioned, could I connect to the Uno via the USB-TTL cable and simply poll the information without causing a reset? This sounds like the best option, since the rest of my program is working correctly?

I read over your sample ArduinoPC2.ino & ComArduino2.py programs and admit I've got much more to learn before I comprehend all that is taking place.

Thanks again for all of your help, RobinGreig

RobinGreig: could I connect to the Uno via the USB-TTL cable and simply poll the information without causing a reset? This sounds like the best option, since the rest of my program is working correctly?

That would make sense if you want the Arduino to run continuously and occasionally connect to it to collect data without causing a reset which would lose a lot of data. My Arduino is controlling my fridge and logging the ON and OFF periods. To be honest it must be at least 12 months since I connected to it :)

However if you only need the Arduino to work while the RPi is connected to it then that would not be necessary and it would be better to write the RPI code properly.

...R

Robin2: That would make sense if you want the Arduino to run continuously and occasionally connect to it to collect data without causing a reset which would lose a lot of data. My Arduino is controlling my fridge and logging the ON and OFF periods. To be honest it must be at least 12 months since I connected to it :)

However if you only need the Arduino to work while the RPi is connected to it then that would not be necessary and it would be better to write the RPI code properly.

...R

Hi Robin2;

Well I do want the Arduino to run continuously and monitor the battery voltages (acutally I have 6 different sets of batteries I monitor, vehicle batteries, and 3 banks from my solar panels). A couple of the vehicles sit quite a bit and so the batteries discharge over time & on my solar panels, if there isn't enough sun, the Arduino can monitor the battery voltages and turn the battery chargers on when & if needed.

Additionally, I only access the Uno to poll the voltages so that I can put them up on my website, so that I can look at them while at work and make sure all is well.

So I changed to communicate between the Raspi & Uno via Serial (/dev/ttyAMA0) & only 2 wires (I'm using 2 resistors right now for the voltage divider to drop the 5V Uno Voltage down for the Raspi. This works great!

I still need to program via the USB-USB cable and that still does reset the Uno of course and that's fine.

I unplug the USB-USB cable and separately power the Uno and run the 2 wire serial connection and no problems.

I'm going to continue to read over the sample programs you provided because I want to understand them better, but at least I'm past this hurdle.

Thanks again for all your help! RobinGreig

RobinGreig: So I changed to communicate between the Raspi & Uno via Serial (/dev/ttyAMA0) & only 2 wires (I'm using 2 resistors right now for the voltage divider to drop the 5V Uno Voltage down for the Raspi. This works great! RobinGreig

Sounds like a good solution.

Don't you need 3 wires - Tx, Rx and GND ?

...R

Robin2: Sounds like a good solution.

Don't you need 3 wires - Tx, Rx and GND ?

...R

Hehehe, yes, you're right, It was a long day.... 3 Wires including GND! Thanks again to the Forum RobinGreig