Help with coding Arduino Uno

Hello everyone.
It's my first time trying to use arduino, and this is a little bit confusing, since I never tried working with C++, but I had some experience with Ruby and C# so i think with a little bit of help I can understand how the programming works.
So, I need to write a code that reads a voltage input and, if it's higher than 2.5v, the code "activates" an output that triggers a relay.
How do I set the variables in the code? And how do I interact with the output pin and read the voltage pin through the code?

Start here: analogRead() - Arduino Reference

Then check out the examples that came with the IDE.

Take a good shot at it and post your results.

1 Like

What @ er_name_not_found said. Is the voltage, to be measured, less than the supply voltage of the Arduino. Inputs will not tolerate voltages more than 0.5V over Vcc. It is easy to drop higher voltages to safe levels with resistor voltage dividers.

The if structure for that. But you may want to add hysteresis to the threshold to keep the relay from chattering when the measured value is close to the threshold.

What relay? Is it active high or active low? You will need a driver for the relay. There are relay modules that have the driver on the module. You will need an external power supply for the relay coil. What is the load? Could it be driven with a logic level MOSFET instead of a relay?

Use digitalWrite(pin) to control the relay.

1 Like

Detecting 2.5V seems very convenient…
Are you really trying to read the on/off state of a 5V signal ?
Just checking…!

Should it be like this?

int analogPin = A3;
int setVoltage = 2.5;
int measuredVoltage = 0;

void setup() {
    Serial.begin(9600);
    pinMode(13, OUTPUT);
}

void loop() {
    measuredVoltage = analogRead(analogPin);
    if (measuredVoltage >= setVoltage) {
      digitalWrite(13, HIGH);
    }
}

I am testing it in tinkercad.com but it is not working properly, the output pin seems to be high all the time, even though the measured voltage is below 2.5V, what could be the problem? Sorry if there is something obviously wrong in the circuit, I am a true newbie here.

Yes, I am using a supply of 12V.

How do I apply this to the code? Using the delay() function?

It is an external relay, I was going to use a 5V SPDT relay, could this be a problem?

No haha the voltage is the amplified signal of a Wheatstone Bridge using a strain gauge.

1 Like

analogRead returns a value between 0 to 1023. 0 for the 0V and 1023 for the 5V so you just have to multiply and divide.

Try the following code:

int analogPin = A3;
int setVoltage = 2.5;
int measuredVoltage = 0;

void setup() {
    Serial.begin(9600);
    pinMode(13, OUTPUT);
}

void loop() {
    measuredVoltage = analogRead(analogPin);
// GERRYMOD
//    if (measuredVoltage >= setVoltage) {
    if (measuredVoltage >= 1023 * setVoltage / 5) {
      digitalWrite(13, HIGH);
    }
// GERRYMOD      
    else {
      digitalWrite(13, LOW);      
    }
  
}

No. Like this.

const byte analogPin = A3;

word
setVoltage = 512, // 2.5V
measuredVoltage = 0
;

void setup ()
{
  pinMode ( 13, OUTPUT );
}

void loop ()
{
  measuredVoltage = analogRead ( analogPin );
  digitalWrite ( 13, measuredVoltage >= setVoltage );
}

@gerivega

Bad idea.

I tried it and it is working now, though I had to change the data type of the setVoltage variable, like @flashko pointed out, int was a bad idea haha. Thanks everyone!

1 Like

To calculate the voltage requires the use of floating point math. Floating point math is slow. You know that the analog read returns 0 to 1023 for 0 to 5V so 2.5V is 512. Use that for the threshold.
Then you do no calculating.

const byte analogInputPin = A0;
const byte ledPin = 13;

const int THRESHOLD = 512;

void setup()
{
   Serial.begin(115200);
   pinMode(ledPin, OUTPUT);
}

void loop()
{
   if (analogRead(analogInputPin) >= THRESHOLD )
   {
      digitalWrite(ledPin, HIGH);
   }
   else
   {
      digitalWrite(ledPin, LOW);
   }
}

No you are not. You are putting 12V into an Arduino and the Arduino has in it a voltage regulator that reduces that to 5V. That is the voltage that the Arduino runs at, that is the voltage you must. It exceed.

You use hysteresis, that is one voltage to turn on the relay and the other to turn it off.

You can not normally connect a relay coil directly to an Arduino’s pin, unless the coil takes less than 20mA. You normally need a transistor to drive it and a diode across the relay coil.
See http://www.thebox.myzen.co.uk/Workshop/Motors_1.html about half way down the page when it discusses driving relays.

The above code written with hysteresis.

const byte analogInputPin = A0;
const byte ledPin = 13;

const int threshold = 512;  // = 2.5V wint 0 - 5V input
const int hysteresis = 100;  // adjust to prevent relay chatter

void setup()
{
   Serial.begin(115200);
   pinMode(ledPin, OUTPUT);
}

void loop()
{
   int analogValue = analogRead(analogInputPin);
   if (analogValue >= threshold + hysteresis) 
   {
      digitalWrite(ledPin, HIGH);
   }
   else if (analogValue <= threshold - hysteresis)
   {
      digitalWrite(ledPin, LOW);
   }
}

consider

const byte pinRly = LED_BUILTIN;
const byte pinInp = A1;

#define Threshold  (1024 * 2.5/12) 

void
loop (void)
{
    digitalWrite (pinRly, Threshold <= analoglRead (pinInp));
}

void
setup (void)
{
    pinMode (pinRly, OUTPUT);
}

may need to protect the input from 12V with a diode or use a voltage divider as suggested