Lately, I have been trying to serially communicate raspberry pi with Atmega32 using UART. The raspberry pi has to calculate some coordinate value and transmit it to Atmega which controls motors. I took some help in writing the code from internet for raspberry pi in python and wrote receiver code for Atmega with the help from its data sheet. I transmitted some character data from raspberry pi. I tried to check whether correct output is coming, by sending the received data to port A of atmega-32 but the output is completely unexpected (some leds glow while some do not at random). Could anyone tell me, what I have been doing wrong?

Transmit section code (Raspberry pi):

import serial
from time import sleep
port = serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=None)

while 1:
    pin = raw_input("Activate pin: ").strip("\r\n")

Receiver section code (Atmega 32):

#include <avr/io.h>
#ifndef F_CPU
#define F_CPU 16000000UL                    // set the CPU clock
#include <util/delay.h>

#define BAUD 9600
#define BAUDRATE ((F_CPU)/(BAUD*16UL)-1)

void uart_init(void);
void uart_transmit(unsigned char);
unsigned char uart_receive(void);

int main(void)
    unsigned char a, num;
    char buffer[10];

    DDRA = 0xff;

    while (1) 
        a = uart_receive();
        PORTA = a;


void uart_init(void)
    UBRRH = (BAUDRATE>>8);
    UCSRB |= (1<<TXEN)|(1<<RXEN);
    UCSRC |= (1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);

void uart_transmit(unsigned char data)
    while(!(UCSRA & (1<<UDRE)));
    UDR = data;

unsigned char uart_receive(void)
    while(!(UCSRA) & (1<<RXC));
    return UDR;
  • You have not mentioned how the code is wrong. What is it supposed to do? What does it do? How do you know it is not working? You should also include photos of all the connections so we can rule out a wiring issue. Jul 31 '17 at 11:21
  • Start with common baud rate , for test purpose very slow rate is better. SInce you have indicated you have some visual indicators - slow baud rate is more humanly detectable. Make sure you handshake is also correct. Sometime loopbacks helps to make sure you are actually connected before you start sending / analyzing your data.
    – Jan Hus
    Aug 1 '17 at 2:17
  • This type of communication usually involves buffering the data. It is better to read the buffer until it is empty or some terminating character is received. Reading one character at a time and introducing software delay is not the best way to collect assynchronous data.
    – Jan Hus
    Aug 1 '17 at 3:37

the baudrate values of the Rpi and the Atmega differs. the Unexpected output maybe due to that. try changing that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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