Arduino Uno / Arduino Nano Serial Communication

Hi, I am experimenting with the Arduiono Uno and Arduino Nano. At the moment I am trying to communicate from the PC to the Arduino over the serial port. But I want to understand the underlying procedure completely. So far I found in the documentation the Arduino is doing the following steps after a reset.

  • Run the bootloader.
  • Looking for an limited amount of time for a new sketch on the serial port.
  • If a sketch is available burn it into the flash memory.
  • Run the sketch.

After reading more information about PC to Arduino communication and also making some experiments I found out the following about the Arduino Nano.

  • The board contains an FT232RL to translate USB and Serial communication.
  • The ATMEGA gets reset if DTR is going LOW.
  • If I open a communication session the ATMEGA gets reset (e.g. open the Arduino IDE Serial Monitor).

Now I got some questions about it.

I am not sure but is this the bootloader for the Uno & Nano ?

If I open a serial connection to the Arduino and transmit some data immediately, the Arduino gets reset and looking for a new sketch. Is this dangerous because the bootloader could mistakenly interpret the data as a new sketch? So I have to wait a moment, until the sketch is starting and setup the UART peripheral again, before send some data?

What exactly makes the FT232RL assert DTR LOW and what exactly happens if I open the serial monitor? Why if I run the following C# program the Arduino gets only reset the first time and after restarting the C# program the Arduino is not resetting anymore until I disconnect and connect the USB plug again?

           SerialPort lSerial = new SerialPort("COM4", 9600); // , Parity.None, 8, StopBits.One

            Console.WriteLine("Press enter to open connection...");
            Console.ReadLine();
            lSerial.Open();
            Console.WriteLine("Connection opened.");

            Console.WriteLine();
            Console.WriteLine("---");
            Console.WriteLine();

            Console.WriteLine("Press enter to close connection...");
            Console.ReadLine();
            lSerial.Close();
            Console.WriteLine("Connection closed.");

            Console.WriteLine();
            Console.WriteLine("---");
            Console.WriteLine();

            Console.WriteLine("Press enter to exit...");
            Console.ReadLine();

Can I use the Win32 API (e.g. CreateFile) with C++ to communicate with the Arduino?

If you are using an Uno or Nano board I would expect the bootloader to be already included so you can just ignore it. Attache either board to your PC and upload your program.

When you start the Arduino after the program is uploaded it will spend a few seconds at startup checking if the Arduino IDE is trying to upload a different program. Assuming it is not then your program will start running.

There is almost no possibility of the bootloader mistaking data for a new program. However when I write a PC program to communicate with an Arduino I have the PC program wait until it gets a message from the Arduino telling it that my Arduino program is running. Usually I put something inane like Serial.println(“Arduino Ready”); in setup() and the PC waits until it receives that before trying to talk to the Arduino.

Have a look at how it is done in this Python - Arduino demo

Any PC programming language can do the same sort of thing.

If I am answering the wrong question please explain more clearly what you actually want to do.

…R

Opening a serial connection will generally cause the serial adapter to assert DTR, which is why opening serial monitor resets the Arduino.

This can be overridden with a lower level serial terminal (I use hTerm - it's got buttons to assert and release DTR and RTS, and icons to show state of the input modem control pins. )

It sounds like your C# code is doing something wrong and it isn't telling the serial adapter to release DTR when it should. Consult the C# library documentation and figure out what you're doing wrong (other than using C#, which is a miserable language)

The Uno and Nano have different bootloader. The Arduino Uno bootloader waits a very short period of time for the upload; the Nano bootloader waits somewhat longer. The one on the Uno (optiboot) is strictly better - it's smaller (so more space for sketch) and uploads faster (it's not uncommon for people to bootload nanos and pro minis as Unos - they use the same microcontroller).

"it's not uncommon for people to bootload nanos and pro minis as Unos" I bootload all my 328Ps as Unos, makes for less confusion when selecting a board type, any project I do is then an Uno board type.

On the Uno, there is a RESET_EN jumper you cut that disconnects DTR so the serial port cannot reset the board. The Nano does not have that.

Thanks Robin2. But I think you misunderstood me somehow. I am just interested in the low level stuff and want to understand each bit of the bootloader procedure and serial transmission process. I am also not using the Arduino IDE, I am using Atmel Studio 7 and assembler.

Thanks DrAzzy. You are right. I played around with hTerm and wrote a small C++ program doing the same as the C# program (You are also right C# is not nearly so pretty as C/C++ but sometimes it's just a faster way testing some stuff) and it works now as expected.

I also read about optiboot but can you show me the link to the default installed bootloader of the Arduino Uno and Arduino Nano? If I search for it I am getting inconclusive results.

If you look in boards.txt you can see what bootloader file is called for each:

uno.bootloader.file=optiboot/optiboot_atmega328.hex

nano.menu.cpu.atmega328.bootloader.file=atmega/ATmegaBOOT_168_atmega328.hex

Both files can be found in the /bootloaders folder of your IDE install. /Arduino1.8.1/hardware/arduino/avr/bootloaders/atmega and /optiboot

osccon: Thanks Robin2. But I think you misunderstood me somehow. I am just interested in the low level stuff and want to understand each bit of the bootloader procedure and serial transmission process. I am also not using the Arduino IDE, I am using Atmel Studio 7 and assembler.

Unless you are thinking of writing your own bootloader I can't say I understand why you need more info than I provided.

You can always program an Arduino using ICSP and not bother with the bootloader but you need an extra piece of hardware for that - another Arduino can be used. That is how I program my Attinys.

...R