1

I'm using Arduino Micro to read from 5 flex sensors and display the corresponding angles to the Serial monitor. I am currently having quite some problems with the oscillating values I am getting from the analogRead(). It doesn't seem to matter whether the pin is connected to a flex sensor or just grounded - the output is oscillating a lot.

Originally everything was being read and outputted just fine but I wanted to have an exact 100Hz sampling frequency and tried to play a bit with Timer Interrupts. And that's when this oscillating behaviour started. I reversed to my original code, which just uses some delay(), and simplified to only read from two pins, but cannot seem to shake off the oscillations.

I think I may have messed up something about ADC when trying to implement Interrupts, but I don't know how to check it or fix it. Please, help me figure out how to fix this!

This is the raw output of analogRead. The drop in values occurs when I bend the flex sensor:

This is the raw output of analogRead. The drop in values occurs when I bend the flex sensor

And this is the resulting calculated angle. Also oscillating:

And this is the resulting calculated angle. Also oscillating.

This is the code I've been using to read the pins:

int fin;
const int input[5] = {A0,A1,A2,A3,A4}; // the analog pins

int flex[5]; // analog signal read
float flexV; 
float flexR[5]; // resistance on the 47k resistor
int angle[5]; // joint angles

const float VCC = 4.98; // Measured voltage of Arduino 5V line
// Measured resistance of the 47k resistors R1-R5
const float R[5] = {45900.0,45900.0,45900.0,45900.0,45900.0}; 

// Calibration values of resistance measured during straight phase and 90 deg bend phase
const float R_STRAIGHT[5] = {37651.0,37651.0,37651.0,37651.0,37651.0};
const float R_BEND[5] = {71783.0,71783.0,71783.0,71783.0,71783.0};

void setup() {

}

void loop() {

  for(fin = 0; fin <= 4; fin++) {
   flex[fin] = analogRead(input[fin]);
   flexV = flex[fin]*VCC/1023.0;
   flexR[fin] = R[fin] * (VCC/flexV - 1.0);
   angle[fin] = map(flexR[fin],R_STRAIGHT[fin],R_BEND[fin],0,90.0);
   delay(1);   
    }
    Serial.print(angle[0]);
    Serial.print(" ");
    Serial.print(angle[1]);
    Serial.print(" ");
    Serial.print(angle[2]);
    Serial.print(" ");
    Serial.print(angle[3]);
    Serial.print(" ");
    Serial.print(angle[4]);
    Serial.print(" ");
    Serial.println(millis());
    delay(6);   
}
1
  • The first graph looks to me like to are plotting two channels interspersed. Commented Jun 25, 2019 at 7:27

1 Answer 1

1

You have nothing in your setup loop. You need to set up all the inputs as inputs.

You setup() should look like this:

void setup() 
 {
   Serial.begin(9600);
   for (int i = 0; i < 5; i++);
    {
      pinMode(input[i], INPUT);
    }
 }

The code cycles through your array "input", setting all the pins as inputs. Try that.

6
  • 1
    As I remember, the pinmode doesn't matter for analogRead, since pinMode(pin, INPUT) will set the pin as digital input. analogRead() will configure the pin to use the ADC, which has nothing to do with digital input. This is only often done for readability, so that the reader directly sees, which pins are inputs and which are outputs.
    – chrisl
    Commented Jun 25, 2019 at 7:06
  • 1
    Gpios are input by default
    – Sim Son
    Commented Jun 25, 2019 at 8:27
  • I think the pins should be input by default? Anyways, I've tried that. No change. I think I somehow messed up the ADC by have no idea how to test it out... Commented Jun 25, 2019 at 9:40
  • You can easily test your ADC by connecting it to a variable power source. You should be seeing 1023 when it's at 5v and 0 when it's at 0v. If you don't have a variable power source use a 10K potentiometer, with the left side connected to 5v, middle pin to your ADC pin, and your right to GND. Turning the knob will produce the same results. You can also use a multimeter to verify the flex sensors are working properly. Commented Jun 25, 2019 at 17:51
  • Tried that out. I'm getting nice 1023 at 5V but anything between 220 and 800 at 0V... Commented Jul 2, 2019 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.