Hello, so I am making a project where I am trying to display the voltage and weight of solid objects weighing from 30 to 1000 grams on the serial monitor. I just need some help on how to display the wight on the serial monitor and how I can display the voltage as well.
Here is my code:
#define FSR A0 // the FSR and 10K pulldown are connected to A0
int ledBLUE=7;
int ledRED=8;
int ledGREEN=9;
void setup() {
Serial.begin(9600);
pinMode(ledBLUE,OUPUT);
pinMode(ledRED,OUTPUT);
pinMode(ledGREEN,OUTPUT);
}
void loop() {
int analogReading = analogRead(FSR);
Serial.print("Force sensor reading = ");
Serial.print(analogReading); // print the raw analog reading
if (analogReading < 10) // from 0 to 9
Serial.println("no pressure");
else if (analogReading < 200) // from 10 to 199
Serial.println(" -> light touch");
digitalWrite(ledBLUE,LOW);
digitalWrite(ledGREEN,HIGH);
digitalWrite(ledRED,LOW);
else if (analogReading < 800) // from 500 to 799
Serial.println(" -> medium squeeze");
digitalWrite(ledBLUE,HIGH);
digitalWrite(ledGREEN,LOW);
digitalWrite(ledRED,LOW);
else (analogReading <= 1000)// from 800 to 1000
Serial.println(" -> big squeeze");
digitalWrite(ledBLUE,LOW);
digitalWrite(ledGREEN,LOW);
digitalWrite(ledRED,HIGH);
delay(1000);
}
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.
Here is your code formatted and properly posted in a code block.
#define FSR A0 // the FSR and 10K pulldown are connected to A0
int ledBLUE = 7;
int ledRED = 8;
int ledGREEN = 9;
void setup()
{
Serial.begin(9600);
pinMode(ledBLUE, OUPUT);
pinMode(ledRED, OUTPUT);
pinMode(ledGREEN, OUTPUT);
}
void loop()
{
int analogReading = analogRead(FSR);
Serial.print("Force sensor reading = ");
Serial.print(analogReading); // print the raw analog reading
if (analogReading < 10) // from 0 to 9
Serial.println("no pressure");
else if (analogReading < 200) // from 10 to 199
Serial.println(" -> light touch");
digitalWrite(ledBLUE, LOW);
digitalWrite(ledGREEN, HIGH);
digitalWrite(ledRED, LOW);
else if (analogReading < 800) // from 500 to 799
Serial.println(" -> medium squeeze");
digitalWrite(ledBLUE, HIGH);
digitalWrite(ledGREEN, LOW);
digitalWrite(ledRED, LOW);
else (analogReading <= 1000)// from 800 to 1000
Serial.println(" -> big squeeze");
digitalWrite(ledBLUE, LOW);
digitalWrite(ledGREEN, LOW);
digitalWrite(ledRED, HIGH);
delay(1000);
}
Notice that, in your if and else if structures, only the one line after the if or else if is executed conditionally. All of the rest of the code is executed unconditionally. If you want the rest of the code to execute conditionally, put the code in curly brackets ({}).
Like so:
else if (analogReading < 200) // from 10 to 199
{
Serial.println(" -> light touch");
digitalWrite(ledBLUE, LOW);
digitalWrite(ledGREEN, HIGH);
digitalWrite(ledRED, LOW);
}
Using a FSR to measure the weight is not accurate, but you will find that soon enough when the sketch works. I think it is best to use a flat hard surface below the FSR and then put a soft rubber that covers the complete FSR on top of the FSR. [EDIT] I was wrong, see the posts below.
There is only one statement after a if condition.
if(ozone > 2000)
Serial.println("This is just one statement");
If you want more statements, then you have to group them between '{' and '}'.
if(ozone > 2000)
{
Serial.println("Too much Ozone");
digitalWrite(alarm, HIGH);
}
A condition with a "else" is not possible. The "else" meanse "everything else" without condition.
else (analogReading <= 1000) // not valid 'C' code
I suggest to turn off the leds below 10.
What happens if the value is above 1000 ? You could turn on all the leds.
When fixing the things that I mention, a working sketch is the result:
// https://forum.arduino.cc/t/force-sensor-resistor-to-display-weight-and-voltage/1077247
#define FSR A0 // the FSR and 10K pulldown are connected to A0
int ledBLUE = 7;
int ledRED = 8;
int ledGREEN = 9;
void setup()
{
Serial.begin(9600);
pinMode(ledBLUE, OUTPUT);
pinMode(ledRED, OUTPUT);
pinMode(ledGREEN, OUTPUT);
}
void loop()
{
int analogReading = analogRead(FSR);
Serial.print("Force sensor reading = ");
Serial.print(analogReading); // print the raw analog reading
if (analogReading < 10) // from 0 to 9
{
Serial.println(" -> no pressure");
digitalWrite(ledBLUE, LOW);
digitalWrite(ledGREEN, LOW);
digitalWrite(ledRED, LOW);
}
else if (analogReading < 200) // from 10 to 199
{
Serial.println(" -> light touch");
digitalWrite(ledBLUE, LOW);
digitalWrite(ledGREEN, HIGH);
digitalWrite(ledRED, LOW);
}
else if (analogReading < 800) // from 500 to 799
{
Serial.println(" -> medium squeeze");
digitalWrite(ledBLUE, HIGH);
digitalWrite(ledGREEN, LOW);
digitalWrite(ledRED, LOW);
}
else if (analogReading <= 1000) // from 800 to 1000
{
Serial.println(" -> big squeeze");
digitalWrite(ledBLUE, LOW);
digitalWrite(ledGREEN, LOW);
digitalWrite(ledRED, HIGH);
}
else
{
Serial.println(" -> something wrong ?");
digitalWrite(ledBLUE, HIGH);
digitalWrite(ledGREEN, HIGH);
digitalWrite(ledRED, HIGH);
}
delay(1000);
}
You can try it in the Wokwi simulation. Click on the link (the bold green text below), then start the simulation with the start button in the upper-middle of the screen, then use your mouse to turn the knob of the potentiometer.
put a soft rubber that covers the complete FSR on top of the FSR.
Not according to this FSR manufacturer:
best results will be achieved when 100% of the force is concentrated within the sensing area, and when 70-85% of the sensing area is loaded. The best way to accomplish this is through using a puck.
Thanks, so a "soft rubber" is too soft. They mention Neopreen in that link, that is soft rubber, but I think that, for example, 2 mm is still too thick. Perhaps a bicycle inner tube would be better. The "puck" they sell is made from Delrin, that is a hard plastic. Not extremely hard, but harder than nylon from what I read online.
Certainly, but usually strain gauges are used (not just one).
The following two links have just about everything you need to know about strain gauges. Then, connect your Wheatstone bridge to an HX711, which your Arduino can read.
With judicious choice of components, that may give you what a load cell would. But it would be very difficult for a novice to design a DIY device that would rival a commercial product's performance.
You need some object that bends according to an imposed mass. Then fix the strain gauge to that object so that it senses the contraction or dilatation of the object surface.
If you really mean mass, not weight, then a much more complicated machine is required.
Your two topics on the same or similar subject have been merged.
Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.
Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.
Repeated duplicate posting could result in a temporary or permanent ban from the forum.
Could you take a few moments to Learn How To Use The Forum
It will help you get the best out of the forum in the future.
No, the thing that caught my eye was that you said
Whereas the manufacturer recommends
As illustrated in the screenshot below. So, definitely not covering the complete FSR.
Also, I would bet dollars to donuts that when they say "neoprene," they aren't referring to the soft foam neoprene such as used in wetsuits and mouse pads. They're referring to solid neoprene in the same form used in sealing washers and, believe it or not, highway and railroad bridge bearings. It can be quite hard - up to 70 Durometer is sometimes used for bridge bearings.
@DaveEvans, thank you, for learning something new I thought that "neoprene" is only the "wetsuit" fabric.
Okay, so a hard plastic, and not completely covering the whole area
No, I am just on a budget so I am trying to reduce the amount I have to buy and I am borrowing the strain gauges from a friend but I was able to manage to get an hx711 so how could I use strain gauges with and hx711 to accuratly measure weight
So I was doing a bit of research and found out you can use Piezoelectric sensor to measure weight accurately, if this is the case what type of Piezoelectric sensor will I need and along with an amplifier . Would very much appreciate if you could add a link as well fo where I can get it