Simple code not doing what i hoped.

Being new to arduino, ive tried an analogue input. My aim was to turn on the built in LED when the input voltage at pin A0 reaches X amount. Ive written the code below, and tried varying values for the threshold figure but cannot get the led to turn on. I have 1 volt going into A0 at the moment but have tried grounding the input and giving it 5 volts to no avail. Im sure theres something simple in my understanding that ive missed but would be graetefull for some pointers.

int analogPin = A0; //pin that voltage is attached to
int ledPin = 13; //led pin
int threshold = 100; // arbitrary value

void setup() {
  // put your setup code here, to run once:
pinMode(ledPin, OUTPUT);//led pin as output pin
}

void loop() {
  int analogValue = analogRead(analogPin);//read value at analog imput pin
  
  if (analogValue < threshold) {ledPin = 1;}//if value at analog pin is less than arbitary value turn led on
  else {ledPin = 0;}//otherwise turn led off.

You're setting the variable ledPin to 0 or 1.

"ledPin" is just a variable, which was equal to the pin number of the pin with the LED on it. After your code starts running, it's 0 or 1.

Nowhere do you actually write a value to the LED pin.
To write a value to a pin, use digitalWrite(pin, value) - so instead of ledPin=0 or ledPin=1, you want digitalWrite(ledPin,0); or digitalWrite(ledPin,1);

I see. I shall try it.

Thank you

And you can avoid this issue in the future by replacing

int ledPin = 13; //led pin

with

const int ledPin = 13; //led pin

or even

const byte ledPin = 13; //led pin

Good Luck!

vaj4088:
And you can avoid this issue in the future by replacing

int ledPin = 13; //led pin

with

const int ledPin = 13; //led pin

or even

const byte ledPin = 13; //led pin

Good Luck!

This post is misleading.
While it's a good idea to use 'const', that won't "avoid the issue".
A 'digitalWrite()' is still needed, as pointed out by DrAzzy.

OldSteve:
This post is misleading.
While it's a good idea to use 'const', that won't "avoid the issue".
A 'digitalWrite()' is still needed, as pointed out by DrAzzy.

It would totally point out the issue at least because with const in there this line:

else {ledPin = 0;}//otherwise turn led off.

would throw an error pointing out the problem instead of compiling and not doing what the OP expected.

Well, it helps the compiler point out the error instead of saying "That's weird but I'll do what I'm told."

The optimization to use a byte is not necessary. The pin numbers in Arduino are ints so it will be upgraded to an int everywhere you use it. Byte is also unsigned so you can't use -1 to indicate 'no pin'. The compiler will see that this const is used as a constant and it will put it directly into the binary without using up any SRAM storage, like a normal variable.

Delta_G:
It would totally point out the issue at least because with const in there this line:

else {ledPin = 0;}//otherwise turn led off.

would throw an error pointing out the problem instead of compiling and not doing what the OP expected.

Fair point. I hadn't looked at it that way.