OiO.lk Blog C# AVR Microcontroller Blinking LED Not Working
C#

AVR Microcontroller Blinking LED Not Working


I am trying to learn embedded electronics using the 8-bit AVR microcontroller and whilst I have experience with electronics and programming I am a noob with embedded systems. The chip I am trying to program is the Atmega168. I am following the Make: AVR Programming book but I am at a complete loss trying to understand why the LED is failing to blink even after following the book.

I have tried troubleshooting both hardware and software side without any success. I am using an arduino as an ISP and the chip has no bootloader. I have tried programming through the Arduino IDE and through VS code terminal. The commands for compilation and uploading complete sucessfully and AVRdude says I have flashed 174bytes.

The circuit I am using to try and program the Atmega168 with arduino as ISP is taken directly from the book I am reading. I have attached a photo to help understand it. There is a red LED which is on and indicating power supply is OK. The green LED is being driven from PBO (Pin 14). I was initially powering the chip from the Arduino 5v power supply but have since in my inexperience and frustration changed to powering from about 3.2 volts from a voltage divider not realising that 5v is within the operating voltage for the chip.

The commands that I am running for compiling the C program and uploading are as follows;

  1. avr-gcc -mmcu=atmega168 -Os blinkLED.c -o blinkLED.elf

  2. objcopy -O ihex blinkLED.elf blinkLED.hex

  3. avrdude -p m168 -c avrisp -P COM4 -b 19200 -U flash:w:blinkLED.hex

After the commands successfully run the LED does nothing. The C program that is being compiled and flashed to the microcontroller is copied directly from the github repo and is as follows;

/* Blinker Demo */

// -------- Macros -------//
#define F_CPU 1000000UL

// ------- Preamble -------- //
#include <avr/io.h>                        /* Defines pins, ports, etc */
#include <util/delay.h>                     /* Functions to waste time */

int main(void) {

    // -------- Inits --------- //
    DDRB |= 0b00000001;            

    // ------ Event loop ------ //
    while (1) {

      PORTB = 0b00000001;          /* Turn on first LED bit/pin in PORTB */
      _delay_ms(1000);                                           /* wait */

      PORTB = 0b00000000;          /* Turn off all B pins, including LED */
      _delay_ms(1000);                                           /* wait */

    }                                                  /* End event loop */
    return 0;                            /* This line is never reached */
}

I have tried different a different mcu when compiling with avr-gcc but this usually results in errors or warnings so I assume atmega168 is correct and not say atmega168P. As stated previous I have tried in mutliple IDE’s and no error warnings when compiling or flashing. All the com port and baud rate settings are good as whenever I change these I can no longer communicate with the microcontrolller through AVRdude. I have checked the wiring multiple times so I assume I have not done anything silly with the wiring and even after compeletely re-wiring according to the book no difference.

I have tried googling and changing code and rewiring and spent hours troubleshooting. Whilst the troubleshooting has helped improve my understanding of the toolchain I can’t actually start anything interesting until I get this working. Given that everything is compiling and uploading OK is making it difficult to understand why it is not working. Any help or advice is very much appreciated.



You need to sign in to view this answers

Exit mobile version