[SOLVED] Wheatstone bridge autobalance

ted
Thu Jan 03, 2019 5:21 pm
Wheatstone bridge
Happy New Year to all.
I intend to make a program which allows to auto balance off Wheatstone bridge by pressing the button.
The circuit.
1.8 M is replaced by second photoresistor and LED which brightness is regulated by PWM pulse width by pressing the buttons. I want to replace those two buttons by 1 button = auto balance. The function should find the minimum value of voltage applied to PA7 which comes from amplifier LM324.

I was searching for similar applications of Arduino functions min/max without luck.
I need help.

Image

#include "EmonLib.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);
EnergyMonitor emon1;
float cur, acur[50], sumc;
volatile int i = 128; //initializing a integer for incrementing and decrementing duty ratio.

void setup() {
pinMode(PB6, INPUT_PULLDOWN);// sets the pin0 as output
pinMode(PB5, INPUT_PULLDOWN);// sets the pin1 as output
// pinMode(PA3, PWM);
//pinMode(PA2, PWM);
pinMode(PA7, INPUT); // I2

analogWrite(PA3, i);

lcd.begin(16, 2); // set up the LCD’s number of columns and rows
Serial.begin(9600);
emon1.current(PA7, 2500);
}
// Serial.begin(9600);
//Serial.println(i);
void loop()
{
emon1.calcVI(20, 2000);
emon1.serialprint();

float Irms = emon1.Irms;

if (digitalRead(PB6) == HIGH)
{
if (i < 255)
{
i++;//if pin PB3 is pressed and the duty ratio value is less than 255
analogWrite(PA3, i); // analogWrite values from 0 to 255
delay(30);
}
}
if (digitalRead(PB5) == HIGH)
{
if (i > 0)
{
i--;// if pin PB5 is pressed and the duty ratio value is greater than 0
analogWrite(PA3, i); // analogWrite values from 0 to 255
delay(30);
}
}
Serial.println(i);
lcd.setCursor(0, 1);
lcd.print("U=");
lcd.print(i); // triming b

lcd.setCursor(0, 0);
lcd.print("H="); //A, PA7
lcd.print(Irms );
}


fredbox
Thu Jan 03, 2019 7:57 pm
I would treat this as two independent voltage dividers. Since the impedance is high, you will need to buffer each divider with an opamp (input to +, – connected to output). The output of these two opamps can feed analog input pins. From there, it is a matter of reading the analog inputs and changing the PWM signal to make the analog values match. For STM32, power the circuit from 3.3 volts.

ted
Thu Jan 03, 2019 10:22 pm
With hardware I have no problems, I am looking for examples how arduino function minimum/maximum is used.
https://www.arduino.cc/reference/en/lan … /math/min/

mrburnette
Fri Jan 04, 2019 3:51 am
[ted – Thu Jan 03, 2019 10:22 pm] –
With hardware I have no problems, I am looking for examples how arduino function minimum/maximum is used.
https://www.arduino.cc/reference/en/lan … /math/min/

http://docs.leaflabs.com/docs.leaflabs.com/index.html

…under Math

min()
max()
abs()
constrain()
map()
pow()
sqrt()


ahull
Fri Jan 04, 2019 12:04 pm
.. one other thing to bear in mind, PWM is PWM, so it is on or off as far as your circuit is concerned, you may need to smooth the PWM to give you a more linear voltage, although the LDR may not be too quick reacting to the flashing light, it will react and go from low resistance to high pretty quickly in sync with the PWM.

This may seem like a silly question, but what is the intended function of this design? Could you use something like a loadcell amplifier (designed for weighing stuff using loadcells which are essentially wheatstone bridges) to do the job for you?


dannyf
Fri Jan 04, 2019 2:32 pm
to auto balance off Wheatstone bridge by pressing the button.

sounds like you want to manually auto-balance a bridge – the first in human history I think, :)

many ways to do it and the simplest would be an analog solution, even if it is implemented via a MCU (using the onboard comparator for example).

but before you do anything, you need to make up your mind as to what exactly you are trying to do.


ted
Fri Jan 04, 2019 4:37 pm
@mrburnette
The link do not have math section.

ted
Fri Jan 04, 2019 4:47 pm
Here is the concept.
PWM pulse width sweep will allow to record minimum value of voltage, mim() will find it, than PWM will generate designated pulse width.

ted
Mon Jan 07, 2019 1:53 am
The first step is almost done.
In the first program the LED’s brightness is changing when the button is pressed=0k,
int led = PB15;
/////////////////////
// int button = PB7; // continuous
const int button= PB12;//
//////////////////////
int buttonState =0;
int brightness = 0;
int brightup = 1;

void setup() {
pinMode(led, OUTPUT);
//pinMode(button, INPUT);
pinMode(button, INPUT_PULLDOWN);
}

void loop() {
analogWrite(led, brightness);

buttonState = digitalRead(button);

if ( buttonState == HIGH ) {
brightness = brightness + brightup;
}
//if ( brightness == 255 )
if ( brightness == 100 )
{
brightness = 0;
}
delay(30);
}


idahowalker
Mon Jan 07, 2019 2:05 am
What’s this mean:

if (ledPin, HIGH) //PC13
{
}


ted
Mon Jan 07, 2019 2:50 am
It is mean, do whatever is in the brackets when logic level on PC13 is HIGH

idahowalker
Tue Jan 08, 2019 1:19 pm
So you are indicating this if (ledPin, HIGH) //PC13
{
}

MoDu
Tue Jan 08, 2019 4:11 pm
Any non-zero value is interpreted as True, for most compilers.

fredbox
Wed Jan 09, 2019 4:30 am
[ted – Mon Jan 07, 2019 2:50 am] –
It is mean, do whatever is in the brackets when logic level on PC13 is HIGH

No, it does not do that. It means if HIGH is true, do whatever is in the brackets.

In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type).
See https://en.wikipedia.org/wiki/Comma_operator#Condition


stevestrong
Wed Jan 09, 2019 8:19 am
Thanks, I learned something new today 8-)

dannyf
Wed Jan 09, 2019 11:11 am
if (ledPin == HIGH) //PC13

ted
Wed Jan 09, 2019 10:29 pm
I make another program and is working as I need, the first step is done.
//int led = PB15;
const int led = PB15;
const int button= PB12;//

// constants won't change. They're used here to set pin numbers:
//const int button2Pin = PB9;
int button2Pin = PB9; // the number of the pushbutton pin
int ledPin = PC13; // the number of the LED pin

// variables will change:
int button2State = 0;

int buttonState =0;
int brightness = 0;
int brightup = 1;

void setup() {

// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(button2Pin, INPUT_PULLDOWN);

pinMode(led, OUTPUT);
//pinMode(button, INPUT);
pinMode(button, INPUT_PULLDOWN);
}

void loop() {

// read the state of the pushbutton value:
button2State = digitalRead(button2Pin);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (button2State == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}

analogWrite(led, brightness);

buttonState = digitalRead(button);

if ( buttonState == HIGH ) {
brightness = brightness + brightup;
}
//if ( brightness == 255 )
if ( brightness == 100 )
{
brightness = 0;
}
delay(30);
}


stevestrong
Thu Jan 10, 2019 7:49 am
You have two variables “volt”, a global (int) and a local (float, in main()).

ted
Thu Jan 10, 2019 2:20 pm
I don’t think the problem is there.

To make more clear, this is a voltmeter program when LED is turning ON at
if(volt <= 1200 && volt >= 1000)


stevestrong
Thu Jan 10, 2019 4:39 pm
If you don’t listen to us, then you are free to do what you want, but don’t expect any support. I am finished here. Good luck.

ted
Thu Jan 10, 2019 6:01 pm
I think it is opposite way, why I should do changes to program which is working properly = last code, the problem was in addition. Thank for your wishes, the results below.
Part 2 is done.
Just put both programs together and auto balance will work.
int currentValue; // global variables are retained on each iteration of loop()
int previousValue;
int analogPin = PB0;
const int buttonPin = PB12; //
int buttonState = 0;
int volt;
int i;
//int volt, avolt[50];
int LED = PB14;
//int volt = 0;
#include <LiquidCrystal.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);

void setup() {
pinMode(buttonPin, INPUT_PULLDOWN);
pinMode(LED_BUILTIN, OUTPUT);
//pinMode(PA7, INPUT_ANALOG);
pinMode(PB0, INPUT);
pinMode(PB14, OUTPUT);
lcd.begin(16, 2);
}

void loop() {
buttonState = digitalRead(buttonPin);
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(200);

float volt = analogRead(PB0);
//volt = (volt * 3.3) / 4095.0;
volt = (volt * 3.3);
lcd.setCursor(0, 0);
lcd.print(volt);
delay(1000);

previousValue = currentValue; // store what was read last time
currentValue = analogRead(analogPin); // get a new reading
//if (previousValue != currentValue)
if (buttonState == HIGH)
if (previousValue > currentValue)

{ // compare them
// do something, they are different

digitalWrite(PB14, HIGH);
LED = 1;

}
else
// do something else, they are the same
{
digitalWrite(PB14, LOW);
LED = 0;

}

}


Leave a Reply

Your email address will not be published. Required fields are marked *