How to change serial buffer size for Arduino Due? - serial-port

I've met this problem that Arduino Due takes more time to complete the serial writing than Mega 2560 or Uno.
Here is the code I'm using to test:
int t1,t2;
void setup() {
Serial.begin(115200);
}
void loop() {
t1=micros();
Serial.println("ABCDEFGHABCDEFGHABCDEFGHABCDEFGHABCDEFGHABCDEFGHABCDEFGH01234567");
t2=micros();
Serial.print("time taken: ");
Serial.println(t2-t1);
delay(500);
}
It takes Arduino Due 5,500 microseconds while it takes Arduino Mega 2560 600 microseconds.
To change the serial buffer size for the Mega, I know the buffer size is defined in the Hardwareserial.h at C:\Arduino\hardware\arduino\avr\cores\arduino. But I could not find the buffer size definition in the corresponding file for the Arduino Due.
Does anyone know how to make the Arduino Due have a faster serial writing speed? We need to transmit the data wirelessly so we could not use the native port although it's very fast.
Thanks!

Well, I was looking for that as well, filesearch revealed:
C:\Arduino\hardware\arduino\sam\cores\arduino\RingBuffer.h - line 28
Having the Arduino 1.5.7 and this works for Arduino Due Programming and Native Port.

for ide 1.6.7 path is:
C:\Users\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.7\cores\arduino

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.

Arduino analogRead() reset microcontroller

I am using Atmega328 with arduino bootloader. My whole code is working fine. Now I need to use analogRead() to get ADC values, but as soon as PC see analogRead(), it restart microcontroller. Here is the sample code.
void setup() {
Serial.begin(19200);
while(!Serial);
Serial.println("Setup finish");
delay(200);
}
void loop() {
Serial.println("Reading analong Values");
uint16_t a = analogRead(A0);
Serial.println(a);
delay(1000);
}
The output is,
Setup finish
�Setup finish
�Setup finish
�Setup finish
�Setup finish
�Setup finish
�Setup finish
�Setup finish
�
I also tried to put delay() before and after it but no vain. How to fix it. Thank you.
Update:
I have tried 0 instead of A0, but no vain.
Update:
The problem all boils down to voltage selection(3.3 or 5V) switch on FTDI programmer. Setting it to 5V works perfectly, but switching it to 3.3V, the problem appears again.
From the Arduino site here:
DEFAULT: the default analog reference of 5 volts (on 5V Arduino boards) or 3.3 volts (on 3.3V Arduino boards)
INTERNAL: an built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328 and 2.56 volts on the ATmega8 (not available on the Arduino Mega)
This clearly shows that atmega328 you are using requires an internal reference to 1.1v when reading off the analog inputs. It is probably restarting because when using analogReference(DEFAULT); the atmega328 doesn't know how to properly decode the signal and it crashes out.
Since analogRead only works on analog input pins, it takes the channel number, not a pin number. Try passing 0 rather than A0. It likely fails because A0 has a higher number (as a digital pin) than the number of analog input channels, causing an out of bounds error.
The inductor between power supply 3.3V and AVcc pin was the problem of analogRead() reset. The purpose of this inductor is mentioned here, Inductor purpose. But short-circuiting the inductor clears the problem.
I recommend that instead of writing uint16_t a = analogRead(A0), you should declare the input of A0 as a variable and call it later in the program.Also, there is a mistake in the void setup() section which is that you wrote the while loop with the condition and the ended the line with a semi colon. You should have written the actions that are to be done if the statement is true between curly brackets

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.

Programming a buffer using ATMEGA328

Im just learning how to use the Arduino, so i bought a Arduino UNO that comes with the ATMega328. And just to start learning i was thinking to use the controller as a buffer:
How would be the code for that? What pins can i use for a RS232 signal?
Thanks a lot.
Uhm... Sorry but.. What? What do you want to do?
If you just want to use it as a buffer you can do this
const int inputPin = 2;
const int outputPin = 1;
void setup() {
pinMode(inputPin, INPUT);
pinMode(outputPin, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(outputPin, digitalRead(inputPin));
}
But this is absolutely useless.
If you want to start from the beginning start reading the examples provided with the IDE. Starting from the classic example (blink a led).
As for the RS232.. You can't connect to a RS232 signal (because the RS232 signal is a bipolar signal which can range from -15V to 15V). If you want you can convert the RS232 voltage levels to UART levels (0-5V) with an IC (the most famous one is the MAX232).
And then.. You can either connect it to the TX and RX pins (but doing so you exclude the USB port) or to two other pins and then use the SoftwareSerial library.
But usually people use the integrated USB-Serial converter on the board: when you connect the Arduino to the PC your computer detects a new serial port..

Unable to get Data from Serial Device using Arduino

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.

Resources