Code to ATMega328P

Hey everyone! im new here, and new with Arduino.

im tryin to make a code to when i put a more than 5Vcc Voltage on 23 pin, the Atmega328P put the pin24 as HIGH, to set a external Relay..
but i cant make it work in Proteus..

the code is this.. theres something wrong? tnxxx

int ledPin = 24;     // output to realy
int analogPin = 23;  // input
int val = 0;        // 

void setup() {
  pinMode(ledPin, OUTPUT);  // pin 24 as output
}

void loop() {
  val = analogRead(analogPin);  // read pin 23
 
  if (analogPin > 512) {
  analogWrite(ledPin, HIGH);
}
}

I wouldn't advise putting more than 5V on any 328P pin.

  1. Proteus is garbage; simulators in general are. The majority of the time when someone posts here about a problem they saw in proteus, it was a problem with the simulator not matching reality.

  2. When referring to pin numbers, use either the name of the pin (eg "Vcc", "Gnd" etc), the port and bit (eg, PC1, PD0, etc), or the Arduino pin number/name (eg, 14 or A0). Never physical pin numbers - I had to go look at the datasheet to translate that into which pin it actually is. It's PC0, aka A0 (ADC channel 0).

  3. This makes no sense "i put a more than 5Vcc Voltage on 23 pin," - More than 5v? More than Vcc? Which?

  4. Do you have external components connected? You must have external components if you need to measure a voltage in excess of Vcc - applying any voltage higher than Vcc or lower than ground to a pin will damage the part (you can go to Vcc +0.5V or Gnd - 0.5; there's an internal diode from than pin to Vcc and Gnd that keeps it from going outside that range, but that diode is only rated for 1mA maximum. If you exceed that, that's how you burn out pins. It looks like you code is written for a 2:1 voltage divider (eg, 10k resistor between ground and A0, and 10k resistor between A0 and the voltage being measured).

Note that if you have an external voltage that can swing outside the supply rails, but you don't need to do analog measurement on it, you only care about whether it is high enough to be considered "high" or not, you can get away with just a resistor, as long as that's sufficient to limit maximum, worst case, current to +/- 1mA. Atmel suggested this as a way to make a budget zero crossing detector: they tied digital ground to the neutral, and the hot to a digital pin through a 1MOhm resistor. One thing I can tell you is - I would never touch something wired like that while it was plugged in, because I have seen multiple outlets wired with the hot and neutral reversed - and unless you checked it with an outlet wiring tester, or touched the "neutral" while grounded (which would be a shocking experience, to say the least), you'd never know; things work fine that way, as long as something else isn't miswired.

Edit: okay, I probably would touch it anyway. A 120VAC shock wouldn't be lifethreatening or anything, just... unpleasant....

hey @DrAzzy and @markd833

sorry, i wasn't know about the pin names..
actly i will do this:

i will use a strain gauge circuit, with amplifiers, bridges and so on to mesure the force of one machine, so if the resultant voltage be more than 2,5Vcc (sorry, its not 5Vcc as i said before) in A0 pin, the pin A1 have to turn to HIGH.

im using only the software to simulate first, i think for small things like this will be no problem.
just need help with this code..

thanks for the answers

Edit:

DrAzzy:
Edit: okay, I probably would touch it anyway. A 120VAC shock wouldn't be lifethreatening or anything, just... unpleasant....

not so bad :]

Oh, and I see your problem in the code, too:

You referred to the pins by physical number, instead of their Arduino names...

pin 24 is A1 (or 15), pin 23 is A0 (or 14). PC1 and PC0 - though you can't use those to refer to it in Arduino (some cores, including all of mine, give defines of the form PIN_Pxn, eg PIN_PC0 - but the official cores don't)

DrAzzy:
Oh, and I see your problem in the code, too:

You referred to the pins by physical number, instead of their Arduino names...

pin 24 is A1 (or 15), pin 23 is A0 (or 14). PC1 and PC0 - though you can't use those to refer to it in Arduino (some cores, including all of mine, give defines of the form PIN_Pxn, eg PIN_PC0 - but the official cores don't)

Thanks DrAzzy, it works now :smiley: