Unable to get Data from Serial Device using Arduino - serial-port

I am trying to read the data from a Ultra Sonic Fuel Sensor(the link).The Baud rate of this device is 9600.The device basically sends data at regular time intervals.I am able to read the output in the PC using Terminal software.Given below is a sample.
Eg:*XD,205B,00,0000,0031,0000,0000,null#
I am trying to connect this device to Arduino through serial port provided in the device and when I see the Serial Monitor,the output is not correct.Given below is the sample.
5320215115451166102572432302302432302302302302432303816623024323023023023024323023023023051822281141463
String incoming_char; // Will hold the incoming character from the Serial Port.
void setup()
{
//Initialize serial ports for communication.
Serial.begin(9600);
Serial1.begin(9600);
Serial.println("Starting Communication with Fuel Sensor");
}
void loop()
{
//If a character comes in from the cellular module...
if(Serial1.available() >0)
{
incoming_char=String(Serial1.read()); // Get the character from the cellular serial port.
Serial.print(incoming_char); // Print the incoming character to the terminal.
}
}
The Arduino is powered from USB and the Device from a 12V supply.
The voltage levels from the device Tx-GND=-5.44V,Rx-GND=-8.22V.
I initially thought the the issue might be because of the voltage range and made a voltage divider circuit and fed Arduino the proportionate voltage.Even that is not working.
So,what is the thing which is going wrong ?Please guide me.

Since you are using Serial1 I am assuming you are using an Arduino Mega?
From your question I would say the issue isn't voltage etc. but more likely to be how you are reading the data. You are assuming that the sensor will be returning char values. Are there any specifications on what is being returned?
I created a similar project using an Arduino. Except my Ultrasonic device was used as a range finder. There are details here. As you can see in the code the range is returned from the sensor as a two byte integer.
You will need to find out what the what the Ultra Sonic Fuel Sensor is returning and read in a similar fashion.

allright i would start by suggesting that you connect this to an analog pin to read. you will have to find the ratio between the fuel hight and voltage by measuring and dividing. then insert the multiplication in the code and you are set it will look like the hight instead of just a voltage

This is just a wild guess since I don't own an Arduino Mega (I have Duemilanove and Uno), but I've worked on projects wherein I've encountered issues similar to what you have. Sometimes adding a delay() on your void loop() block helps and gives it enough time for the arduino to read the bytes from the buffer. For 9600 baud rate, it usually takes about 1 ms to read 1 byte so adding a delay is necessary.
void loop()
{
//If a character comes in from the cellular module...
if(Serial1.available() >0)
{
incoming_char=String(Serial1.read()); // Get the character from the cellular serial port.
Serial.print(incoming_char); // Print the incoming character to the terminal.
}
delay(100);
}

Thank you all ! for your Value inputs.The problem was, I was trying to connect RS232 Serial(Works with Negative Voltages) to TTL serial interface(Works with 0 to some Positive Voltages) used in Arduino. Apparently,I was supplying negative voltages to Arduino whereas it was expected to give Positive voltages. So, got a RS232-to-TTL connector and it worked,finally.

Related

Problem with serial communication between Arduino Mega2560 and ESP32

Good morning everyone.
I am trying to establish serial communication between an arduino mega and an esp32, in both I am using hardware serials.
In the arduino the uart3 in the esp the uart2. I have checked the pin connections several times.
I also adapted the arduino's tx signal to the esp32 with a level shifter.
Essentially I need to send a string to the esp32.
The code of the arduino is as follows:
String InvioDatiESP() {
String da_passare = ("hello!!!");
return(da_passare);
}
void setup() {
Serial.begin(9600);
Serial3.begin(115200);
}
void loop() {
Serial3.println(InvioDatiESP());
Serial.println(InvioDatiESP());
delay(1000);
}
I create the string in a function since it is a reduced version of the actual code in which the string is to be composed.
The code of Esp32 is as follows:
#define RXp2 16
#define TXp2 17
void setup() {
Serial.begin(115200);
Serial2.begin(115200, SERIAL_8N1, RXp2, TXp2);
}
void loop() {
Serial.println(Serial2.readString());
}
I correctly set the boudrate in both serial ports on the IDE to verify communication.
The thing I notice that makes me doubt that the problem is related only to the ESP32 reading the string is that in the serial port of the ESP32 in the IDE while the program is running, blank lines are printed on the screen exactly every 1000ms, as if the data is received but not interpreted correctly.
How could I solve this in your opinion?
Thanks in advance for the answers!
SOLVED, the lever shifter was disturbing the signal too much, seen with the oscilloscope, I used a resistive divider and everything works perfectly, thank you all the same!
EDIT: Can you try to lower the boudrate? Check to see if with a lower one it will start decoding properly.
Your problem is not with the code, it's with the hardware. The arduino Mega2560 is using 5V logic level and ESP32 is using 3.3V.
You need to do some level shifting to be able to communicate.
You can take a look at this article to learn more about it.
Hope it helps.

Wire Library - just the first byte device address is being transmitted

I am starting to study the Wire library (no previous Arduino Wire library experience), I read some info taken from here.
As you all know, this really simple example changes the value of a AD5171 digital potentiometer via I2C. Written by Nicholas Zambetti and Shawn Bonkowski, demonstrates use of the Wire library.
I just copied and reduced the code below a little from the example. I am an experienced assembler and C/C++ programmer and hardware developer/designer. Although several I2C devices like DS3231 RTC, etc. work fine using standard Arduino libraries, the mentioned example doesn't work for me in my working NANO board. What am I doing wrong?
This code should transmit:
first the I2C protocol device address - Start / 8 + 1bits
test instruction data byte
variable 1 test byte constantly incremented
I2C Stop condition
The only byte I can see in my oscilloscope is just the first one (please see picture below). The 2 data bytes are not being transmitted. If I reduce the transmission to just the step #2 instruction single data byte, the same result is shown.
#include <Wire.h>
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
}
byte val = 0;
void loop()
{
Wire.beginTransmission(44); // transmit to device #44 (0x2c)
Wire.write(byte(0x55)); // sends instruction byte
Wire.write(val++); // sends potentiometer value byte
Wire.endTransmission(); // stop transmitting
delay(50); // some time delay for my oscilloscope
}
This is what this code produces:
I see transferred data as 01011000 1. I hope the is right decoded. Why you not zoom it more for better reading? It is hard see data levels on raising clock edges. This match to address 44, 0x2C . As you can also see that you get NACK on the end of address octet. So this means no device with transmitting address is on the bus.
You can get error code about sending process from endTransmission function as return int. Send this value to serial monitor.
Error codes you can see in arduino documentation
uint8_t err;
err= Wire.endTransmission(); // stop transmitting and get status
You can also use sketch i2c_scanner from arduino Wire examples to discovery right address of your device.

Android Things I2C read from arduino 3v3

I try to communicate, read and write, from Arduino - slave - to RPi - master - with Android Things.
If i R/W, with a level converter, from RPi to Arduino 5v (16Mhz), everything works fine.
So i decide to eliminate the level converter, and use a 3v3 Arduino mini pro (8Mhz).
The write works fine, but when i try to read from the Arduino, the signal stops.
5v_16Mhz
After the Setup to 9, 0 address, and reads to 9, the signal still low and received the data. No problem.
3v3_8Mhz
After the Setup to 9, 0 address, and read to 9, the signal goes high and the data stop.
I used the same example for the Slave:
#include <Wire.h>
byte RFID[20] = {9,8,7,6,5,4,3,2,1,1,2,3,4,5,6,7,8,9,1,2};
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onRequest(requestEvent); // register event
Wire.onReceive(receiveEvent); // register event
Serial.begin(115200); // start serial for output
pinMode(13, OUTPUT);
}
void loop() {
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
Serial.println("Master ask");
digitalWrite(13, HIGH);
delay(250);
Wire.write(RFID, 20);
digitalWrite(13, LOW);
}
// function should be executes whenever data is received from master
// this function is registered as an event, but it's called every time the RPi
// call the Device.
void receiveEvent(int howMany) {
while (0 < Wire.available()) {
byte RTC_syn = Wire.read(); // receive byte
Serial.println(RTC_syn);
}
}
I really don't know how drives the signal high...
Someone can help me?
If i R/W, with a level converter, from RPi to Arduino 5v (16Mhz), everything works fine.
So i decide to eliminate the level converter, and use a 3v3 Arduino mini pro (8Mhz).
The write works fine, but when i try to read from the Arduino, the signal stops.
This is because level converter you had in the 5V/3.3V version does more than shift the voltage. It also acts as a nice high-impedance buffer between the two devices that helps keep the signal driven and avoids loading effects.
Without the buffer, your bus is likely experiencing a bit of loading. You can try to combat this by adding stronger pull-up resistors. The RPi3 has 1.8k pull-up resistors on the I2C lines, which generally works but can be marginal depending on the input impedance of the slave device. The Arduino Mini has pads to install I2C pull-ups but there are none by default.
The recommended pull-up resistance for a pure 3.3V I2C bus is closer to 1k, so you likely just need to add some stronger pull-ups between SCL/SDA and +3.3V. Anything you add will be in parallel to the RPi3 resistors so factor that into your calculation. For example, adding 4.7k resistors brings the effective resistance down to about 1.3k.
If you are unable to solve it with pull-ups, you can achieve the same buffer effect without level translation by using a line driver IC (random example).
If the level converter works, you should stick with it.
Communication protocols like I2C encode data into a series of logic HIGH and logic LOW signals. What does HIGH / LOW mean? It depends on the devices. For the majority of embedded devices, logic LOW will be ground, 0V.
For Arduinos and Raspberry Pis, the source voltage is different (3.3V versus 5V). This difference can lead to several potential issues.
The 5V signal is too high for the Arduino to handle, causing the Arduino to stop working or reboot
The 3.3V signal is not strong enough to be interpreted as logic HIGH. Embedded devices have circuits that round signals to HIGH/LOW, and the thresholds may not be entirely even. A 5V input may only accept 4.5V or higher, interpreting everything else as LOW or in an indeterminate state.

Garbage data received if transmiting some data from the same Softwareserial port

Following is the code to transmit data from Srial monitor to other device and receive from other and print in the serial monitor
the below code is working fine if i Don't transmit the data in between (i.e without this line)
esSe.write("test");
but when i write this line and upload.The device receive the data from the Serial monitor and the String("test") also.
But the data transmitted by that device( or the data received by arduino becomes garbage)
I even tried to flush the transmitting buffer of the device through[esSe.flush()] but no change in the result
this the code i have used
#include <SoftwareSerial.h>
SoftwareSerial esSe(2, 3);
void setup() {
Serial.begin(9600); while(!Serial);
esSe.begin(9600); while(!esSe);
}
void loop() {
Serial.flush();
while(Serial.available())
esSe.print((char)Serial.read());
//esSe.write("test");
//esSe.flush();
while(esSe.available())
Serial.println((char)esSe.read());
//delay(10);
}
and when i give Delay of approx 50 milli second it works fine
and in delay of 10 it give data and some garbage data too.
SoftwareSerial cannot transmit and receive at the same time (see #4 below). This answer lists the choices for serial ports, in this order of preference:
1) HardwareSerial (you're using this for debug).
2) AltSoftSerial is very efficient and reliable, but it requires pins 8 & 9 on an UNO.
After that, any other pins can be used with one of these two software serial libraries:
3) NeoSWSerial is less efficient than AltSoftSerial, but it is much more efficient than SoftwareSerial. It only supports baud rates 9600, 19200 and 38400, and it support simultaneous TX and RX. I maintain this library.
4) If you must use a different baud rate, SoftwareSerial is the last choice. It blocks interrupts for long periods of time and can interfere with other libraries. It cannot transmit and receive at the same time.
If you can move to pins 8 & 9, change to AltSoftSerial. If those pins are not available, change to NeoSWSerial.

Two port receive using software serial on Arduino

i am having trouble getting data from two sensors using two software serial ports with an arduino board. I noticed a similar question might have been asked before but the answers suggest it can't be done and I know fully well it can based on the example here (http://arduino.cc/en/Tutorial/TwoPortReceive)!
I am using an arduino ethernet. The devices I am trying to get data from include a GPS and an IMU both from sparkfun.
I can get data from either devices using just on software serial port but as soon as I add the second software serial port, neither ports will work. I can't use the hardware serial port because that is being used byt another device.
My code is exactly similar to the example:
#include <SoftwareSerial.h>
SoftwareSerial portOne(7,8);
SoftwareSerial portTwo(5,6);
void setup()
{
Serial.begin(9600);
portOne.begin(9600);
portTwo.begin(9600);
}
void loop()
{
portOne.listen();
while (portOne.available() > 0) {
char inByte = portOne.read();
Serial.write(inByte);
}
delay(500);
portTwo.listen();
while (portTwo.available() > 0) {
char inByte = portTwo.read();
Serial.write(inByte);
}
Serial.println();
}
Anyone with any ideas?
This code will not work, or will work poorly if it works at all. SoftwareSerial only has one internal buffer. Yes, you can have multiple SoftwareSerial objects in existence, but only one of them controls the internal buffer. When any RX pin gets asserted, that generates an interrupt, but only the listen()ing RX pin gets checked for a start bit.
What's really needed is the ability to check on multiple pins when an interrupt comes along from the start bit. Then you'd have to set up pointers to the appropriate data structures. It would be complicated, but possible.
Or maybe just give up on interrupt-driven reception, and spin on checking both/all of the RX pins, and start the receive based on the pin you see. Be forwarned that this code has much hair, and you WILL need an oscilloscope to make it work.
I'm having a similar problem, which is why I found your sensor. After talking it over with my co-workers, we've decided to read our sensors in rotating order. Our sensors report the current state of the sensor, and not specific events, so it's okay if we lose some reports. So we'll read from port 1, then read from port 2, then port 1, etc. Our sensors spit out lines of text, so we know when to switch to the next sensor.
The referenced example only actively listens to one port at a time. The recommended solution would be to upgrade to an Arduino Mega (https://www.sparkfun.com/products/11061) which has 4 hardware serial ports.
In order to simultaneously support two software serial ports is going to require a lot of the CPU resources. It also be a difficult design and excessive programming time far outweighing the cost of $58 + shipping.
Looking at you code again it occurs to me that you are immediately checking for characters after your portOne.listen command. At 9600 baud it will take approximately 1ms for the first character to arrive, your while test will have been completed and the portTwo.listen command executed long before the first character arrives.
For testing purposes try adding a 1-2 ms delay after the portOne.listen command and see if you get a character.
As an example (untested and note, if port one is sending characters with no intercharacter gaps, the first while will never fail, preventing reading portTwo characters):
void loop()
{
portOne.listen();
delay(2);
while (portOne.available() > 0) {
char inByte = portOne.read();
Serial.write(inByte);
delay(1);
}
portTwo.listen();
delay(2);
while (portTwo.available() > 0) {
char inByte = portTwo.read();
Serial.write(inByte);
delay(1);
}
Serial.println();
}
Don't use while ......
Use:
{ portOne.listen();
if (PortOne.available() ) {
ricevo = myPort1.read(); }
// delay(2); // ridiculos waiting time
// delay(1); // extra ridiculos waiting time
Than 500 ms is a too big time for switching, no time.....

Resources