Serial Communication Issue

Hello guys,

I was wondering if you could help me with an issue with my serial communication. I am trying to control a few 12V valves (two to be precise) using relays and read from two analog pin sensors. Depending on what the sensor readings are I have written a simple code that turns on one valve or the other and sends over serial the reading of the sensor. Everything works as I had planned except when the valves turn on or off at which time I get a bunch of junk on the serial monitor.

For example, the Arduino might send 100y100 instead of:
100
100

Any ideas? Thank you in advance,
Nick

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. The system in the 3rd example will be most reliable.

If that does not help then you need to post your program.

...R

Hi Robin,

Thanks for the response and for pointing me to the Serial Input Basics guide. I'm still a little unsure of why everything works fine until my valves come on. Perhaps you could take a look at my code (posted below)?
While I have the same problem when looking at the values in the serial monitor, ultimately I am plotting the sensor data in Matlab and my matlab code gets completely messed up when the arduino doesn't send characters of the assumed length and range over serial.

Here is my code:

#include<math.h>
//Valves
int ValveH = 13;
int ValveD = 12;
int Valve4 = 10;
int Valve5 = 9;
int count=0;

//Analog Inputs
int PotPin = A0;
int Input_Mix = A1;
int Input_Dry=A2;

//All functions
int H_Setting_to_serial(int PotPin){
int Potpin_Val = analogRead(PotPin); // value between 0 and 1023
int Humidity_Setting= (20 * Potpin_Val / 1023); // This
Humidity_Setting=Humidity_Setting*5;
int Hum2Serial=Humidity_Setting+100;
//Serial.flush();
//Serial.println(Hum2Serial);
return Humidity_Setting;

}

int Humidity_Reading_Dry(int Input_Dry){
int Dry=analogRead(Input_Dry);
// Convert to a voltage
float Humidity_Dry_Voltage= Dry * (5.0 / 1023.0);
float Humidity_Dry2= (Humidity_Dry_Voltage- 0.958)/0.0307 ;
int Humidity_Dry= (int) floor(Humidity_Dry2 + 0.5);
return Humidity_Dry;
}

int Humidity_Reading_Mix(int Input_Mix){
int Mix=analogRead(Input_Mix);
// Convert to a voltage
float Humidity_Mix_Voltage= Mix * (5.0 / 1023.0);
float Humidity_Mix2= (Humidity_Mix_Voltage- 0.958)/0.0307 ;
int Humidity_Mix= (int) floor(Humidity_Mix2 + 0.5);
return Humidity_Mix;
}
int send_to_serial(int Value_to_Send){
int Serial_Val=Value_to_Send+100;
if(Serial_Val>99 && Serial_Val<201)
{
Serial.flush();
Serial.println(Serial_Val);
return 1;
}
}

void setup()
{
pinMode(ValveH, OUTPUT);
pinMode(ValveD, OUTPUT);
pinMode(Valve4, OUTPUT);
pinMode(Valve5, OUTPUT);
digitalWrite(ValveH, HIGH);
digitalWrite(ValveD, HIGH);
digitalWrite(Valve4, LOW);
digitalWrite(Valve5, LOW);

Serial.begin(9600);
}

void loop()
{
int Humidity_Setting=H_Setting_to_serial(PotPin); // Reads the potentiometer value and sends it through serial
int Humidity_Dry=Humidity_Reading_Dry(Input_Dry); // Reads the values from the humidity sensors
int Humidity_Mix=Humidity_Reading_Mix(Input_Mix);

// SIMPLE CONTROL
if(Humidity_Mix<Humidity_Setting){
digitalWrite(ValveH,LOW);
digitalWrite(ValveD,HIGH);
}
else{
digitalWrite(ValveH,HIGH);
digitalWrite(ValveD,LOW);
}
// SEND THE VALUES TO SERIAL FOR MATLAB TO READ.

send_to_serial(Humidity_Setting);

delay(500);
send_to_serial(Humidity_Mix);

delay(500);

}

Sorry. I misread your Original Post and assumed you were receiving data to control the valves.

How are you powering the relays? If they are drawing more than 40mA from an I/O pin they may be upsetting the Arduino.

Another possibility is electrical interference caused by pulses in the switched circuit.

What type of relays are you using?
Make a simple drawing showing how everything is connected and post a photo of the drawing.
Also post a photo of your project so we can see how everything is laid out.

...R

Thanks again for your response. My solenoid valves are 12V valves so I'm using two relays to power them. I am assuming that there might be some interference from the pulses because it only occurs when they switch from on to off and vice versa not when they are resting whether powered or not. Is there anything I can do about the pulses? Maybe adding a diode, capacitor or resistor? Here is a schematic of my setup, hope it helps:

Image from Reply #4 so we don't have to download it. See this Image Guide

...R

I don't understand the diagram.

Where is the Arduino?
Where is the power supply?

...R

Pins 13,12 and 11 are the signals to the relays R1, R2 and R3 respectively. I have an external power supply for the relays (5V adapter) and an external power supply for the Valves (12V adapter). I also have 3 analog inputs, A5 is a potentiometer, A4 is a sensor and A3 is also a sensor. In the diagram attached the arduino is represented by the leftmost table.
In my code when V1 turns on V2 turns off and vice versa. It is when this operation happens that I get a weird response from the Arduino.
Thanks again and forgive my beginningness, this is my first post

It would be much better (probably for you also) if you made a drawing showing the Arduino properly with the relevant pins labeled. It is much too easy to misinterpret written or spoken circuit descriptions.

And it is ESSENTIAL to see a drawing with all the power connections shown clearly because the problem you describe sounds like it is related to how the system ins powered.

AND (in Reply #3) I asked you to post a photo of your project as well as to provide details of the relays you are using. Please post a link to the datasheet for the relays.

...R

Quick notes on EMI protection in case that is the issue:

1 - use twisted pair wiring for all power connections
2 - use correctly sized bypass caps on all power busses