2

I'm trying to use the serial communication to work between a naked AVR Atmega328p and a computer (OS X and Linux Ubuntu).

I can program the atmega (using a pololu programmer), I make it blink a LED on a breadboard, it works fine.

I am using the Arduino libraries to facilitate the task. And they seem to work fine (using delay, digitalRead, digitalWrite, etc).

I use a USB-to-TTL CH340 chip bought on internet. The CH340 works fine, it is recognised both on Ubuntu and OS X, and the loopback test works (Put a jumper between the TX and RX line, launch screen, type something and it comes back in the terminal), so I assume the CH340 is properly recognise and works.

Then, I plug the CH340 on my board. The 5V on the + column of the breadboard, the GND to the - column. The circuit works, my LED blinks as requested. The loop function looks like this:

void setup() {
  Serial.begin(9600);
  pinMode(9, OUTPUT);
}

void loop() {
  digitalWrite(9, HIGH);   // sets the LED on
  delay(30);                     // waits for a second
  digitalWrite(9, LOW);    // sets the LED off
  delay(70);                     // waits for a second
  Serial.println("loop end");
}

As you can see, at the send of the loop I send a string on the serial line. Now, I see the blue LED blinking on the USB-to-TTL converter, so it is definitely receiving data. But when I try to connect like so:

screen /dev/cu.wchusbserialfd110 9600

on OS X, or

screen /dev/ttyUSB0 9600

on Ubuntu, nothing is displayed. I just get a black terminal.

Is the Arduino Serial works with Atmega328 ? I suppose so, as it is the same microcontroller than on the Arduino. Any idea of what I am doing wrong?

Update:

More details to answer Christ Stratton question:

My low fuse is 62: 01100010. So, according to the specifications, the internal clock is used and set at 1MHz.

I programmed the Atmega using my own test bed, a Pololu programmer (as described in the book Practical AVR Microcontrollers: Games, Gadgets, and Home Automation with the Microcontroller Used in Arduino) and avrdude (through the use of platformio). This is the command line used by platformio to flash the ROM:

/Users/[user]/.platformio/packages/tool-avrdude/avrdude -v -p atmega328p -C /Users/[user]/.platformio/packages/tool-avrdude/avrdude.conf -c avrispv2 -B 10 -e -b 115200 -P "/dev/cu.usbmodem00149121" -U flash:w:.pioenvs/pololu/firmware.hex

According to avrdude documentation, -B 10 means bitclock is set to 10us (microseconds) so a clock at speed 100Khz.

Now if I set this -B option to 1 (1Mhz), I can't program the Atmega anymore. Avrdude just timeout:

avrdude stk500v2_ReceiveMessage(): timeout
7
  • I think it is necessary to consider the possibility that the Arduino's bootloader is setting up the UART in some way that the Arduino Serial library assumes, and does not do itself. I believe this is plausible as IIRC the Arduino bootloader will always be run before any uploaded (Arduino) program is started. Can you put the Arduino bootloader onto your ATmega328, and then try to upload to it using your USB-UART? You might need to fiddle with the reset to your ATmega to get it into the right state as autoboot might not work (unless you can wire that up correctly too)
    – gbulmer
    Commented Sep 17, 2016 at 20:29
  • Definitely a question for the Arduino StackExchange. Or even Ask Ubuntu..
    – pipe
    Commented Sep 17, 2016 at 21:36
  • @gbulmer your theory is incorrect. There is absolutely no dependency whatsoever between the bootloader and main program, and it is well known that Arduino code does not in any way require a bootloader to be present. Commented Sep 18, 2016 at 5:58
  • 2
    What is the clock source and frequency of your circuit? What was the clock source and frequency of the Arduino board configuration you used to build? If there is a mismatch all the serial baud rates will be off. If they are off by a power of two, you may be able to temporarily work around the issue by setting your computer to a baud rate that differs by the same factor, but it would be better to make the board configuration match the board. Commented Sep 18, 2016 at 6:01
  • @ChrisStratton, I tried to answer your question. I think I understand what you mean. The Arduino libraries are using some constants to deal with time dependent functionalities and those constant varies according to the board you want to program. If you compile you code with a ucontroller with 8MHz as target and run it on a 1Mhz, your 9600 baud rates will not be achieved. My guess is that either these constants are derived from the -B parameter or from another part of the avrdude configuration. Commented Sep 18, 2016 at 8:45

1 Answer 1

1

The clock rate of your board must match the clock rate defined in the board file you used to compile your sketch. If it does not, the baud rate will differ by the same factor.

If the actual baud rate so achieved is a valid one, you can temporarily achieve communication by setting the computer to that baud rate, but all delays and other clock-rate related functions will be off.

You should use or create a board file entry that matches your board, or use a standard clock source / clock divier setting for which there already is an entry.

1
  • Indeed, just needed to update the platformio.ini file used by platformio to configure avrdude. Commented Sep 19, 2016 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.