Atmega8A's analog pin

I'm trying to insert an Arduino IDE sketch to an (bootloaded) Atmega8A along with a DIP Switch (5 channel), as an analog switch, for a budget-saving project, and I'm using the AnalogInput program to test its analog reading (with a help from a TM1637 7 Segment to get its reading value).

#include <TM1637Display.h>
#define CLK 6
#define DIO 7
TM1637Display display(CLK, DIO);

const int ledPin = 13;
int previousA0Value = 0;

void setup()
{
    pinMode(ledPin, OUTPUT);
    Serial.begin(115200);
    display.setBrightness(0x0f);
}

void loop()
{
    int A0value = analogRead(A0);
    display.showNumberDec(A0value);
    if ( A0value != previousA0Value ) {
        Serial.print("value = "); Serial.println(A0value);
        previousA0Value = A0value;
    }
    digitalWrite(ledPin, HIGH);delay(500);
    digitalWrite(ledPin, LOW); delay(500);
}

This works fine with an Arduino (Nano model), but the Atmega8A doesn't get to read the analog value. What's the reason for this difference and what should I do to circumvent this?

The components I have currently would be the 22pF capacitor, 100nF capacitor, 10 uH inductor, and 16 MHz crystal. I've tried the low=pass filter circuit for the Atmega8A, but it still doesn't work, dunno what I did get wrong for the following? (pics below, without the 7 Segment)

left resistor between power gnd and MCU gnd?
A0 grounded?

From the schematics I found, yeah the A0 is grounded (in Arduino).

link broken

I don't see wires from the AVR GND pins going to GND, other than through a large resistor?

Would be this one then for the pic:

Yeah, did that because previously with Arduino, connecting the GND (to bus strip for other needs) makes the DIP Switch and analog reading) not working, so thought it's the same for Atmega8A, but if this is wrong then how do I fix this?

The AVR needs power, which means +5 and GND to both sets of pins.
Debugging the Analog part comes second. Right now the dip switch looks wired wrong as well; you probably want:

image

if i read Fritzing picture right, it looks right now wired exactly as on your schematic

The second picture (with nano) looks OK, as long as the bus of the protoboard isn't also connected to GND (as it was in the first pic.)

Keep in mind that the GND and 5V pins of the Nano are SOURCES of power for other things, if the board is powered by USB, so you can connect them to the switch and resistor ladders to provide voltages there. But when you have the bare chip, the 5V and GND signals are SINKS of power to the AVR, and need to be connected to a supply, and the supply should also connect to the switches/resistors.

Yeah, for now the analog can be read, thanks.

Just to clarify more, if it's made into a PCB or such, should both of the Atmega8A's GND pin got connected to power source, with one of the GND pin being made parallel with the one connecting to ANALOGIN's parallel R6, or have one of Atmega8A's GND pins connected to power source, but another one only to R6?

Both GND pins connected to the power source.
(Well, one common GND trace that connects to both AVR GND pins and the "bottom" of R6. Typically there would be a "ground plane." The mega8 is essentially the same as the ATmega328 in an Uno or Nano, so see any of the existing PCBs for an example.)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.