I am controlling an oxygen sensor with my arduino UNO. I am using the provided Arduino UNO sample code from the manufacturers website. The code works great, I can read the oxygen no problem.
One issue I have is that when I leave it running on my laptop, the computer will sometimes go to sleep when it is not plugged in and when I come back to wake up the computer the UNO stops working. The serial monitor has no new data and the only way to fix this is to unplug the USB and plug it back in. This by itself is not an issue, I can work around it.
My problem now is that replugging in USB doesn't do anything. I also have a MEGA. Earlier I was able to plug that in to reset the serial port selection window and then plug the UNO back in. This managed to fix the problem once, but now not even this will work.
I have tried using every USB port, to no avail. I even tried running the code on the MEGA and no matter what I do there is nothing coming up on the serial port and the LED on the O2 sensor does not flash like it is meant to. I restarted the laptop and nothing changed. I even uninstalled and reinstalled the IDE from scratch.
Here is the Sensor Datasheet, I am using the default UART communication.
Here is the code for those who cannot find it in the link I posted:
//This code was written to be easy to understand.
//Modify this code as you see fit.
//This code will output data to the Arduino serial monitor.
//Type commands into the Arduino serial monitor to control the EZO-o2 sensor.
//This code was written in the Arduino 2.3.2 IDE
//An Arduino UNO was used to test this code.
//This code was last tested 9/24
#include <SoftwareSerial.h> //we have to include the SoftwareSerial library, or else we can't use it
#define rx 2 //define what pin rx is going to be
#define tx 3 //define what pin tx is going to be
SoftwareSerial myserial(rx, tx); //define how the soft serial port is going to work
String inputstring = ""; //a string to hold incoming data from the PC
String sensorstring = ""; //a string to hold the data from the Atlas Scientific product
boolean sensor_string_complete = false; //have we received all the data from the Atlas Scientific product
float o2; //used to hold a float number that is the o2
void setup() { //set up the hardware
Serial.begin(9600); //set baud rate for the hardware serial port_0 to 9600
myserial.begin(9600); //set baud rate for the software serial port to 9600
inputstring.reserve(10); //set aside some bytes for receiving data from the PC
sensorstring.reserve(30); //set aside some bytes for receiving data from Atlas Scientific product
}
void loop() { //here we go...
if (Serial.available()) { //if a string from the PC has been received in its entirety
inputstring = Serial.readStringUntil(13); //read the string until we see a <CR>
myserial.print(inputstring); //send that string to the Atlas Scientific product
myserial.print('\r'); //add a <CR> to the end of the string
inputstring = ""; //clear the string
}
if (myserial.available() > 0) { //if we see that the Atlas Scientific product has sent a character
char inchar = (char)myserial.read(); //get the char we just received
sensorstring += inchar; //add the char to the var called sensorstring
if (inchar == '\r') { //if the incoming character is a <CR>
sensor_string_complete = true; //set the flag
}
}
if (sensor_string_complete == true) { //if a string from the Atlas Scientific product has been received in its entirety
Serial.println(sensorstring); //send that string to the PC's serial monitor
/* //uncomment this section to see how to convert the o2 readings from a string to a float
if (isdigit(sensorstring[0])) { //if the first character in the string is a digit
o2 = sensorstring.toFloat(); //convert the string to a float so it can be evaluated by the Arduino
if (o2 >= 22) { //if the o2 is greater than or equal to 22%
Serial.println("high"); //print "high" this is demonstrating that the Arduino is evaluating the o2 as a number and not as a string
}
if (o2 <= 20.95) { //if the o2 is less than or equal to 20.95%
Serial.println("low"); //print "low" this is demonstrating that the Arduino is evaluating the o2 as a number and not as a string
}
}
*/
sensorstring = ""; //clear the string
sensor_string_complete = false; //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
}
}
The laptop won't collect data while sleeping, and probably drops the USB serial connection as well. If so, the USB connection has to be re-established for the Uno to transmit data.
My solution is to keep the laptop plugged in to a wall socket and turn off sleep for plugged-in mode.
I am well aware that the UNO won't work when the computer is asleep, that isn't my issue. As I said, if that was the only issue I can work around it. My Arduino is in a weird state where no matter what I plug it into it won't work. The sensor has a solid green light meaning its in standby mode and the serial port remains blank even though the code is uploaded.
For help on this forum, please read and follow the instructions in the "How to get the best out of this forum" post, linked at the head of every forum category.
If you could be clearer on what exactly I am missing that would be much more useful than telling me to read a whole instruction manual. That being said, I added the code for those who are averse to clicking my provided link and I also added the datasheet to the component am using (also linked). I believe my original post is very specific. If you have any further questions about clarity I will happily answer them =D !
I am very new to Arduino, so could you explain what a "null terminated c string" is? This code is what was provided by the O2 sensor's manufacturer and it was working previously.
No wires have come loose or changed places since I started. The "ON" LED is solid green and the one labeled "L" flickers rapidly for a second or 2 when I first plug it in.
It was back before the laptop first went to sleep.
Also, I tried uploading a different script and that seems to work, there is stuff showing up in the monitor. When I go back to this code I am in the same spot (no output).
I just added this: myserial.print('C, 1'); myserial.print('\r');
to the setup (I think this is the correct way to do it) and I still see the same thing, BUT I thought maybe the issue is the device itself, so I changed the first if loop to this:
if (Serial.available()) { //if a string from the PC has been received in its entirety
inputstring = Serial.readStringUntil(13); //read the string until we see a <CR>
myserial.print(inputstring); //send that string to the Atlas Scientific product
myserial.print('\r'); //add a <CR> to the end of the string
Serial.println(inputstring);
inputstring = ""; //clear the string
}
and now when I send commands to the device I can see what's happening in the monitor. This works so now I know the serial monitor is not frozen. Unfortunately I still don't know what the issue is.
My O2 sensor was reading the rooms oxygen without issue, but when my laptop fell asleep it ceased communicating with the arduino. I reconnected the USB and tried to reestablish communication between my serial monitor and my arduino. Currently my sensor has a solid light (which was flashing) and the serial monitor is blank (which was streaming with O2 concentrations every second). When I send a command into the message bar the RX light flashes, so it seems like I am able to send information to the UNO.
My problem is that I am not receiving any information from the sensor.
I am not sure if this is an issue with the arduino not sending the information to the computer or if the problem lies somewhere in the sensor. If it is the later then this is probably out of the scope of this forum and I will need to take it up with Atlas Scientific customer support.
Would that indicate that the sensor is in standby mode? I think that's what the datasheet is indicating.
When your system gets stuck in this unresponsive state, are you able to disconnect +v from the sensor briefly. Does the sensor start sending readings again?
No, that is not the correct way. Single quotes are used for a single char. 'C', ',', ' ', '1' is 4 chars which is a string that needs to be enclosed in double quotes
myserial.print("C, 1");
Do yourself a favor and go into the IDE Preferences and turn compiler warnings to "All"
It would have alerted you
C:\sketch_jan29a.ino:3:16: warning: multi-character character constant [-Wmultichar]
Serial.print('C, 1');
^~~~~~
For anyone wondering what happened here is the solution. I contacted customer support and turns out it was a hardware issue. When the computer went to sleep it switched from UART mode to I2C mode and I had to manually reset it to UART mode again. Thanks to customer support I set it up so that it wont happen again.