Arduino Code to Calculate V-out and Display in the Serial Monitor

Wondering if I could get some help with this? Kind of stuck and having a brain freeze with my son...

Sure. What have you done, so far? Oh, and, do you know the electronic formulas for the resistor bridge? You can't code it without those.

It is a common school problem that shows up a regular basis.

Using words like "wheatstone bridge volts" in the,


Could produce a result set.

A collection of info can be had by using the words, "arduino wheatstone bridge volts" in a internet search thingy.

We probably started over a few time, but this is what we have now:

void setup() {
// put your setup code here, to run once:
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
Serial.begin(9600);

}

void loop() {
// put your main code here, to run repeatedly:

// read the input on analog pin 0:
int sensorValue1 = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage1 = sensorValue1 * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage1);

Serial.println(sensorValue1);

   // read the input on analog pin 1:
int sensorValue2 = analogRead(A1);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage2 = sensorValue2 * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage2);

Serial.println(sensorValue2);

   // read the input on analog pin 2:
int sensorValue3 = analogRead(A2);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage3 = sensorValue3 * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage3);

Serial.println(sensorValue3);

   // read the input on analog pin 3:
int sensorValue4 = analogRead(A3);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage4= sensorValue4 * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage4);

Serial.println(sensorValue4);

   // read the input on analog pin 4:
int sensorValue5 = analogRead(A4);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage5 = sensorValue5 * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage5);

Serial.println(sensorValue5);

}

Good. Now what is the problem?

Might work better

float voltage2 = float(sensorValue2) * (5.0f / 1023.0f);

if you want floats.

Anyways what's the problem?

Why? The first expression on the right will already be recognized as a float calculation.

I've found being more descriptive about floats to cause me less issues.

The 'f' designation will force low precision arithmetic on a processor that has double precision floating point, won't it?

...and still waiting for a description of the OP's problem

I do not know. I know with floats and an Uno/Mega I've run into issues where a float becomes an int and that following certain 'rules', I've learned in dealing with that issue, the numbers remain a float.

I would need to see the actual example to comment. I've never experienced any deviation from the normal L-R algebraic parsing, considering the interpretation of type.

I have a lot of code that depends on it, and I feel confident about it because as far as I know, it's standard.

The key is the order of operations (which you should know anyway, to be really safe). An atomic expression (i.e. containing no results of other expressions), always follows the strict rules of type promotion/cast.

It is the expression that determines the cast, and the order of operands is not relevant. For example both:

33 + 1.0

and

1.0 + 33

result in a float..
The order of operations might fool you if you're not paying attention, for example:

1.0 + 3332 * 10000

The multiplication has higher precedence so it's evaluated first, the second and third operands are recognized as integer types so it overflows because int arithmetic is the default with literal expressions like this.

But,

1.0 + 3332 + 10000

Here the order of operations is L->R and so the first evalution is '1.0 + 3332' which is evaluated as the float (or double depending on the environment), '3333.0'. Then the result will be '13333.0'.

I had discovered the float thingy back in 2015 when I first started programming, which was with an Uno. Since then, I've stuck with the tried-and-true method and have not had issue since.

I do appreciate the education, as I wrote I've only been programming for a few years, all self-taught.

Still waiting for the description of the OP's issue.

I see where you're coming from. It's just that, the additional notation clutters the code somewhat. It's actually fine to add some technically redundant specifiers, if it clarifies the code. But it's like salt in a recipe, greatly improves the dish, but not in excess.

I wish I had your knowledge of ESP32. :slight_smile:

1 Like

Sorry for the delayed response. Where my son and I are hung up is the wiring based on the instructions and code above. We have a pentometer (but the instructions call for 4?), resistors (not sure which type but we have several options), a bread board, Adruino Uno. Not sure if we need anything else. Any input would be greatly appreciated!
Thanks

The problem is we are stuck on the physical wiring based on the instructions to get the proper serial.monitor readings.

Well, it seems to me, you are stuck without the 3 additional potentiometers... unless I'm missing something... you can't wire things that don't exist.

I agree with your assessment. We did find two potentiometers in our kit. Maybe if someone can help with the wiring schematic by assuming we did have all four? My son can pick up another two and we can plug these in once we can figure out the schematic. Thanks for your help.

One pot can represent r1 and r2 and the other pot represent r 3 and r 4.

You could also substitute fixed resistors for the missing potentiometers...

What exactly do you mean, "help with the wiring schematic"? You posted it in the first message. Do you mean, "help wiring a circuit that follows the schematic"? A schematic is a symbolic representation of an actual circuit, but there is no great mystery to it. There is a one to one correspondence between every wire and every component that is represented, and the ones that exist in the physical circuit. So constructing it on a breadboard, for example, is just a matter of folowing and obeying the wire connections that are in the schematic. It may be inside out, upside down, shorter, longer, or look like a bowl of spaghetti, but it will correspond.

Nobody here can really do that for you, without being there and reaching over your shoulder, or spending a lot of sweat time that you should be dedicating to it. It's really an important part of the assignment, the learning how to do that.

We can help you, but it should work like: you try something, show us and we can comment and offer further and perhaps ongoing suggestions and guidance.

If you are never building an actual circuit, you need to at least research the formulas and document those before you begin coding.

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