Unable to program ATmega8a-PU using Arduino Uno as ISP

Hey Everyone!
this is my first post on these forums and my first ever attempt to program a bare micro controller IC. Since i do not have an AVR programmer and it is impossible for me to acquire one where i live , my only choice is to use an Arduino Uno as ISP. I have tried several ways such as using Optiloader's bootloader and also using the example available in Arduino IDE called Arduino as ISP. In both cases uploading the bootloader seems to be successful but i can't upload my blinker program. I get the following error while trying to follow this tutorial:

avrdude: stk500_getsync(): not in sync: resp=0x00

it does 10 attempts(takes a long time) and fails.

so I thought , why would i use a bootloader? let's try uploading hex files directly from avrdude and for that purpose i followed this tutorial.
This is what i type in the cmd:

avrdude -c arduino -b 19200 -p m8 -P COM5 -U flash:w:ATmega8-test.hex
and this is the response:
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.04s

avrdude: Device signature = 0x1e9307 (probably m8)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "ATmega8-test.hex"
avrdude: input file ATmega8-test.hex auto detected as Intel Hex
avrdude: writing flash (360 bytes):

Writing | ################################################## | 100% 0.74s

avrdude: 360 bytes of flash written
avrdude: verifying flash memory against ATmega8-test.hex:
avrdude: load data flash data from input file ATmega8-test.hex:
avrdude: input file ATmega8-test.hex auto detected as Intel Hex
avrdude: input file ATmega8-test.hex contains 360 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.48s

avrdude: verifying ...
avrdude: 360 bytes of flash verified

avrdude: safemode: Fuses OK (E:FF, H:C5, L:BF)

avrdude done. Thank you.

it seems like the upload was successful right? but here is the simple code i'm uploading:

#define F_CPU 16000000UL

#include <avr/io.h>
#include <util/delay.h>

void main()
{
       // Turn on the clock for port D
	DDRD |=  (1 << PD5);

	while(1)
	{
		// switch pin 5 of port D
		PORTD  ^= (1 << PD5);
              //wait for half a second
		_delay_ms(500) ;
	}
}

i have written this code in Atmel studio(called microchip studio these days) , compiled it and am trying to upload the hex file in the Bin folder of the project to my ATmega8a.

according to the official datasheet of ATmega8A, PD5 is the 11th pin on the micro and has no default function and seems to be a GPIO. next i connect an LED in such a way that the - side goes to the common ground(powered by an Arduino uno 5v without an additional regulator) and the positive side goes through a resistor to PD5 but the LED does not blink which means my program is not uploaded successfully? what am i doing wrong?

oh and before i forget here are the connections:
pin1--> through a 9.5k resistor to VCC
pin1--> also connected through a different connection to pin 10 of Arduino
pin 7 --> 5v from arduino
pin 8 --> Arduino GND
(there is a 0.1 Uf capacitor between GND and VCC pins next to pins 7 and 8)
pin 9 -->16 Mhz crystal
pin10 --> 16 Mhz crystal
(there are two 20pico farad capacitors between each leg of the crystal and GND)
pin 17 --> Arduino pin 11
pin 18 --> Arduino pin 12
pin 19 --> Arduino pin 13
pin 20 --> +5
pin 22 --> GND

I tried removing the 9.5 k resistor to VCC and restarting the IC by disconecting Arduino 5 volts and reconnecting it to no avail(thought maybe this would change the boot mode to flash)
i also tried connecting the positive leg of the LED to every single pin of the micro controller thinking maybe PD5 is not the 11th pin , the LED would only turn on when connected to pins 1 , 7 and 20 obviously because they are connected directly to VCC.

update:
ok so i must be doing something wrong in the program , or perhaps i am uploading the wrong hex file , i don't know. Because according to this picture :

pin 11 on ATmega8 is actually pin5 for Arduino so i just wrote this program in Arduino:

void setup() {
  // initialize digital pin 5 as an output.
  pinMode(5, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(5, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(5, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

and now the LED blinks. But a strange thing happened , tired as i was, i made a mistake and initially the program was like this:

void setup() {
  // initialize digital pin 5 as an output.
  pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(5, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(5, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

notice how i was putting pin 13 on output but was switching pin 5's state? normally it shouldn't have worked , but somehow it was working?! like the LED was changing states but was very dim which made me realize something was wrong! as u can se in the picture pins 11 and 19 are very close so maybe there is a huge noise on my breadboard or something?
(after changing the code the LED now blinks with max brightness :D)

That is because you were turning the internal pullup resistor on and off. This is expected behaviour.

1 Like

You are right. It is written in the documentation :slight_smile: I'm also new to the Arduino "language" and find it interesting that it will automatically set the pin on output mode for you but with the pull up resistor!

It does not set it to output mode. Check the datasheet and see the basics of the IO pin and how it can be configured. There are a few registers involved; next you can look at the source code of pinMode and digitalWrite and match it against the datasheet.

1 Like