2

I'm trying to send data from ATmega328p to Raspberry Pi 3 with UART using Pis gpios.

The problem is Pi does not seem to be getting any data from the microcontroller.

Here is how I wired the microcontroller to Pi:

  • ATmega328p VCC to Raspberry Pi 5 volt pin. changed to 3.3 volt pin
  • ATmega328p GND to Raspberry Pi ground pin.
  • ATmega328p RESET to Raspberry Pi GPIO #12.
  • ATmega328p SCK to Raspberry Pi GPIO #24.
  • ATmega328p MOSI to Raspberry Pi GPIO #23.
  • ATmega328p MISO to Raspberry Pi GPIO #18.
  • ATmega328p TXD to Raspberry Pi RX pin.
  • ATmega328p RXD to Raspberry Pi TX pin.

Here is the C code for microcontroller:

#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
#define BAUD 9600UL
#define BRC ((F_CPU/16/BAUD)-1)

int main(void){ 
 UBRR0H =(BRC >> 8);
 UBRR0L = BRC;

 UCSR0B = (1 << TXEN0);
 UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);

 while(1){
  UDR0 = '8';
  _delay_ms(1000);

 }
 return 0;
}

And the python 2.7 script for Pi:

import serial


port = serial.Serial("/dev/ttyAMA0", baudrate = 9600, timeout=5)

port.flush()

while True:

    rcv = port.read(10)

    print "AVR is sending" + rcv

With sudo raspi-config I have enabled serial port hardware and made login shell to be not accessible over serial.

EDIT

I've tested if UART pins are any functional by hooking Pi pin 8(TX) to pin 10(RX) and running a python script which I copied from this tutorial http://www.elinux.org/Serial_port_programming .

Here is the script itself:

import serial

port = serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=3.0)

while True:
    port.write("\r\nSay something:")
    rcv = port.read(10)
    port.write("\r\nYou sent:" + repr(rcv))

The script is running but nothing is appearing on the screen. Seems like the script is unable to write to the port.

I've also double checked that serial is enabled in hardware and serial console is disabled.

In /boot/config.txt enable_uart=1 is set.

3
  • Do you have any debugging tools available such as an oscilloscope to verify if any data is being sent from the AVR's Tx pin? Your AVR code looks the same as my UART library except I have a check to ensure the Transmit buffer is ready to receive new data before moving the data into UDR0 while(!( UCSR0A & (1 << UDRE0)));
    – pfl
    Jul 18, 2017 at 15:59
  • The GPIO pins of the Pi are not 5V tolerant you risk destroying the pin or your entire Pi. Jul 19, 2017 at 0:37
  • I used ISR(USART_TX_vect) to light up an LED when transmission is complete. It appears that microcontroller does transmit the bytes.
    – user70416
    Jul 19, 2017 at 10:19

1 Answer 1

2

With a 1MHz clock speed on the AVR your selected baud rate will not work because the actual baud rate after integer clock division will be at too much of an error compared to your desired baud rate. It will still transmit but your receiver will not recognise it.

It is possible to use 9600 but you need to write a 1 to U2X0 in UCSR0A, you will then need to do BRC*2 as well.

See this site for a useful AVR baud rate calculator.

Edit - Configuring the Serial Port

Dug this out from some of my old documentation as well.

From command line type:

$sudo raspi-config

Go to Advanced Options > Serial interface and select yes to enable, exit settings menu and reboot if prompted.

Type:

$sudo nano /boot/cmdline.txt

Remove the part that reads:

console=serial0,115200

Then save and exit.

Now use a terminal program such as screen or minicom to try and talk to the Pi.

6
  • I've changed the BAUD to 4800, still no success... I'm guessing that UART pins on Pi are simply dead, nevertheless ./pintest shows that all pins are ok.
    – user70416
    Jul 20, 2017 at 4:24
  • There is a lot of protection built into the Pi to prevent you from killing it when experimenting so I highly doubt 5V has killed it. It sounds like it is a problem with the way you have set up your serial port on the Pi. Could you please follow this guide which also gives some useful info on the port. And then use a terminal program, such as minicom, to talk to the AVR. This will verify if you have comms or if there is a problem with your python script.
    – pfl
    Jul 20, 2017 at 8:10
  • See my edit in the answer before you try the guide I linked in the previous comment
    – pfl
    Jul 20, 2017 at 8:20
  • Yes ! It did work ! In the guide you recommended I read that AMA0 port is mainly used for Bluetooth on Rpi 3 so I configured minicom to listen to S0 port and hooked Pis Rx pin to ATmegas TX and it did transmit ! However, when run a python script to write and receive it doesn't work, maybe it's just something wrong withe the script itself. I haven't tested the to transmit from Pi yet but I'm glad that receiving works now ! Thank you very much pfl for provided help !
    – user70416
    Jul 20, 2017 at 11:39
  • Sounds obvious but did you definitely change the line port = serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=3.0) to use /dev/ttyS0? and are you running the script as sudo?
    – pfl
    Jul 20, 2017 at 13:10

Your Answer

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