Hot answers tagged

73

Contrarily to other answers, I'd rather stay away from String for the following reasons: dynamic memory usage (that may quickly lead to heap fragmentation and memory exhaustion) quite slow due to construction/destruction/assignment operators In an embedded environment like Arduino (even for a Mega that has more SRAM), I'd rather use standard C functions: ...


49

Why do people settle? People settle because it is more than fast enough. The most common use is just to print some stuff on a terminal for debuggin. 9600 baud is 960 characters per second, or 12 x 80 character lines per second. How fast can you read? :) If your program is using the serial port for bulk data transfer, you would choose not to settle. ...


40

Know this is old but I ran onto it during my search for Nano(V3)'s not uploading so thought might help someone else. Problem is the bootloader - Arduino IDE BUT I Found an easy solution (right under my nose). I realized that my nano's had been uploading just fine then I had finally updated the Arduino AVR Boards from 1.6.20 to 1.6.21. I didn't think ...


37

ardprintf is a function that I hacked together which simulates printf over the serial connection. This function (given at the bottom) can be pasted in the beginning of the files where the function is needed. It should not create any conflicts. It can be called similar to printf. See it in action in this example: void setup() { Serial.begin(9600); } void ...


33

The Arduino uses the RTS (Request To Send) (and I think DTR (Data Terminal Ready)) signals to auto-reset. If you get a serial terminal that allows you to change the flow control settings you can change this functionality. The Arduino terminal doesn't give you a lot of options and that's the default. Others will allow you to configure a lot more. Setting the ...


24

This function can be used to separate a string into pieces based on what the separating character is. String xval = getValue(myString, ':', 0); String yval = getValue(myString, ':', 1); Serial.println("Y:" + yval); Serial.print("X:" + xval); Convert String to int int xvalue = xvalue.toInt(xval); int yvalue = yvalue.toInt(yval); This Chunk of code ...


22

The problem is specifically pins 0 and 1. Although they can be used as regular digital IO pins, they also serve as the RX and TX pins for the Uno's serial port. The USB connection (for uploading sketches etc.) is routed to the same pins internally. Unfortunately that means anything connected on pins 0 and 1 can interfere with the serial connection, ...


22

Serial.begin(9600) doesn't actually print anything. For that you'd want to use Serial.print("Hello world!") to print the text "Hello world!" to the serial console. Rather it initializes the serial connection at 9600 bits per second. Both sides of the serial connection (i.e. the Arduino and your computer) need to be set to use the same speed serial ...


21

The Arduino ADC clock speed is set in ..arduino-1.5.5\hardware\arduino\avr\cores\arduino\wiring.c Here is the relevant part #if defined(ADCSRA) // Set A/D prescale factor to 128 // 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range. // XXX: this will not work properly for other clock speeds, and // this code should use F_CPU to ...


21

A picture is worth 1000 words, so they say, (1024 words if you work with computers) so I'll post some pictures ... I set up my Uno to send "Fab" at 9600 baud and captured the results on a logic analyzer. The parts shaded in red are the "idle" period between bytes. From the above graphic note that the Tx (transmit) data line is normally high (1) until it ...


20

There are some good applications for visualizing the serial data, including: MegunoLink ($29.95; Lite free) - plotting, logging, programming, reporting and more. MakerPlot ($39) - digital & analog plotting, monitoring, custom interfaces and more. ArduinoPlot (free) - simple plotting. arduino-plotter (free) - easy, light-weight plotting with support ...


20

From the Arduino site for Serial.write and Serial.print: Serial.write() Writes binary data to the serial port. This data is sent as a byte or series of bytes; to send the characters representing the digits of a number use the print() function instead. Serial.print() Prints data to the serial port as human-readable ASCII text.


19

Serial.write is more down to earth , it is simple and fast, it is made to talk binary, one byte at a time. example: Serial.write(0x45); // will write 0100 0101 to the cable Serial.print in the other hand is more versatile , it will do the conversion for you from ASCII to binary it also can convert to BIN/ HEX/OCT/DEC but you need to specify a second ...


17

You appear to have a "pro micro" style board in which the USB communication is directly sourced from the main ATmega32u4 processor, rather than generated as serial data and then forwarded to a distinct USB-serial converter as on traditional Arduinos. Your question could have been resolved much more quickly if you had clearly stated the type of board you ...


16

Asynchronous serial (usually referred to as serial) communications is used to send bytes from one device to another. A device could be one or more of the following: Arduino PC GPS RFID card reader LCD display Modem Other Clock rate and sampling of data Unlike SPI / USB / I2C serial communications does not have a clock signal. The sampling clock is an ...


16

This answer lists the 4 basic choices: HardwareSerial, always the best. Simply use the pre-defined Serial variable. On some Arduinos, there are extra HardwareSerial ports, called Serial1, Serial2, etc. The Nano only has Serial. AltSoftSerial, the best of the software serial libraries. Only one instance is allowed, and it is must be used on one of the ...


15

Is it possible to build a HID device (like a keyboard) using an Arduino uno? yes it is! And there's a great tutorial on how to do that over here At the time being, I have button inputs on the Arduino giving outputs on the serial line. So, how can I transform my current firmware into something that can behave like a HID keyboard? As the tutorial ...


14

I wouldn't normally put two answers to a question, but I only just found this today, where you can use printf without any buffer. // Function that printf and related will use to print int serial_putchar(char c, FILE* f) { if (c == '\n') serial_putchar('\r', f); return Serial.write(c) == 1? 0 : 1; } FILE serial_stdout; void setup(){ Serial....


14

For hardware serial ports you can see in HardwareSerial.cpp that the buffer size varies depending on the amount of RAM available on the particular AVR: #if (RAMEND < 1000) #define SERIAL_BUFFER_SIZE 16 #else #define SERIAL_BUFFER_SIZE 64 #endif For a software serial port in SoftwareSerial.h the receiver buffer size _SS_MAX_RX_BUFF is defined as ...


14

As already pointed out, your Arduino is saying too much too fast. Adding delay() will slow it down, but still it keeps yelling at Processing. Ideally, you want Processing to ask for the value when it's convenient, and then receive one answer from your Arduino. Enter SerialEvent(). As opposed to loop() on your Arduino and draw() in Processing, everything ...


14

In a typical C++ program, you would use the typeid operator, like this: std::cout << typeid(myVar).name(); However, that requires a compiler feature called Runtime Type Information (RTTI). It's disabled in the Arduino IDE, presumably because it tends to increase the runtime memory requirements of the program. You can get more information about the ...


14

To clear an array you would do: for( int i = 0; i < sizeof(data); ++i ) data[i] = (char)0; or memset(data, 0, sizeof(data)); , which does the same thing using a library function. However, because strings of characters (not referring to 'String' objects here) are terminated by a zero byte, only the first byte needs to be zeroed: data[0] = (char)...


14

There are a lot of ways to write a serial protocol depending on what functionality you might want and how much error checking you need. Some of the common things you see in point to point protocols are: End of message The simplest ASCII protocols just have an end of message character sequence, often \r or \n as this is what gets printed when the enter key ...


14

The Arduino Nano cannot do 800 kb/s. As you can see in the source code, the bit duration is rounded to the nearest multiple of 8 CPU cycles. In your case, it is rounded to 3 × 8 CPU cycles, which yields a baud rate of 666.666 kb/s. That is 16% too slow, an error too large for any communication to be possible. At 400 kb/s ...


14

The Arduino core has a 64 byte transmission buffer. Characters are sent out of that buffer by the hardware. When you blast lots of characters out of serial the first 64 just get put into that buffer - almost instantly. Once that buffer is full your sketch blocks until the hardware has sent a character to make room. Since it's sending right from the ...


13

This solved it for me. Download this driver Install it Run sudo nvram boot-args="kext-dev-mode=1" Reboot Serial ports now showed up in the Arduino IDE and also when I used ls -1 /dev/tty* Hopes this helps someone. Reference: This thread


13

If you insist on top performance, the best thing would be to use a macro for that: #define Sprintln(a) (Serial.println(a)) Then instead of Serial.println(F("Hello world!")); write Sprintln(F("Hello world!")); etc. To deactivate the Serial printing, define the macro empty: #define Sprintln(a) This will have the preprocessor remove all debugging ...


12

The truth is always in the datasheets, the schematics and the code: The Arduino UNO actually uses the /DTR line to trigger a reset, as you can see on the following datasheet:


12

You're right -- it doesn't work in most cases, and will almost always return true. The one board where it's actually functional is the Leonardo. According to the official documentation: On the Leonardo, if (Serial) indicates wether or not the USB CDC serial connection is open. For all other instances, including if (Serial1) on the Leonardo, this will ...


11

You could do something like the following, but please take into account several things: If you use readStringUntil(), it will wait until it receives the character or timeouts. Thus, with your current string, the last position will last a little longer, as it has to wait. You can add a trailing & to avoid this timout. You can easily check this behavior ...


Only top voted, non community-wiki answers of a minimum length are eligible