how do I get stable readings from the piezo sensor?

Hi guys, I'm somewhat puzzled, it seems like a simple task, but I'm at a dead end or I made a mistake somewhere. The project is like this, several metal shields connected to each other through thick rubber gaskets, a piezo sensor and an arduino are attached to each, the connection between the arduins is via rs485. The signal from the piezo sensor is fed to the arduino input via a diode bridge of Schotke diodes and a 10nf capacitor, pulled to the ground through a 1M resistor. This is my peak detector. The task, when the ball hits the shield, is to read the readings and determine which shield the ball hit. In principle, everything turns out, except that every third time it determines a hit is not correct. :slight_smile: to localize the problem, I assembled a test shield, attached an electromagnet to the center of it that knocks on the shield with the same force, placed sensors around, in theory all sensors from impact to impact should give the same readings, but no, the spread on the same sensor from impact to impact is more than 40%, which is a lot. Now I think that since I am only interested in the magnitude of the impact, put a high-pass filter on the input so that the reaction would be only to low frequencies. Here is my code, maybe you can tell me and the case can be solved by working with it.

#include <TimerOne.h>
#include <BackgroundADC.h>


#define PIN_RED_LED 3 // Red Led (o, _-_)
#define PIN_GREEN_LED 5 // Green Led (o, _-_)
#define PIN_BLUE_LED 6 // Blue Led (o, _-_)
#define PIN_DE 13  //  RS485 (o, _-_)
#define PIN_BLOCK_IN 7 // address block in (i, -_-)
#define PIN_BLOCK_OUT 8 // address block out (o, -_-)

#define TIME_OUT_EXCH 32 //  (* 16 мкс)
#define BEFORE_TRANSFER_PAUSE 2 //  (мкс)
#define AFTER_TRANSFER_PAUSE 20 //  (мкс)
#define FILTER_BLOCK_TIME 100 //  (* 200 мкс)

byte dt1, dt2, dt3, dt4, dt5;
byte mode=0; // 
byte prevTime, relTime;

volatile byte hitForceValue=0, hForceValue; // Kick Value
byte shieldAdress=51; // place address  

volatile boolean flagBlockLow=true; // block signal low level
byte blockCounter=0;

void setup() {

  pinMode(PIN_DE, OUTPUT);
  digitalWrite(PIN_DE, LOW);
  pinMode(PIN_RED_LED, OUTPUT);
  pinMode(PIN_GREEN_LED, OUTPUT);
  pinMode(PIN_BLUE_LED, OUTPUT);
  digitalWrite(PIN_RED_LED, LOW);
  digitalWrite(PIN_GREEN_LED, LOW);
  digitalWrite(PIN_BLUE_LED, LOW);

  pinMode(PIN_BLOCK_IN, INPUT_PULLUP);
  pinMode(PIN_BLOCK_OUT, OUTPUT);
  digitalWrite(PIN_BLOCK_OUT, LOW);
  
  // led with reset
  analogWrite(PIN_RED_LED, 255);
  delay(200);
  analogWrite(PIN_RED_LED, 0);
  analogWrite(PIN_GREEN_LED, 255);
  delay(200);
  analogWrite(PIN_GREEN_LED, 0);
  analogWrite(PIN_BLUE_LED, 255);
  delay(200);
  analogWrite(PIN_RED_LED, 128);
  analogWrite(PIN_GREEN_LED, 128);
  analogWrite(PIN_BLUE_LED, 128);
    
  TIMSK0=0; // timer stop 0

  TCCR0A= 0b10100011;
  TCCR0B= 0b00100100; // 16 мкс
  TCCR2A= 0b00100011;
  TCCR2B= 0b00000110; // 16 мкс

  // ADC setting
  pinMode(A0, INPUT);  
  ADCSRA |= (1 << ADPS2);                     //
  ADCSRA &= ~ ((1 << ADPS1) | (1 << ADPS0));  //
      
  Timer1.initialize(50);  // 
  Timer1.attachInterrupt(timerInterrupt, 50);  // 
  
  Serial.begin(57600);
  
}

void loop() {

  switch(mode) {

    case 0:
      //------------------ 
      if( Serial.available() != 0 ) {
        dt1= Serial.read();
        prevTime= TCNT0;

        if( (dt1 & 0xc0) == 0 ) {
          // 
          mode=1;
        }
        else if( (dt1 & 0xc0) == 0x40 ) {
          //  
          mode=5;
        }
        else if( (dt1 & 0xc0) == 0x80 ) {
          // kick reset
          mode=9;
        }
        else {
          // 
          mode=10;
        }

      }
      break;

    case 1:
      //------------------ read kick value 2 bites
      if( Serial.available() != 0 ) {
        dt2= Serial.read();
        if( dt2 == (dt1 ^ 0xe5) ) {
          // right
          // check address
          if( shieldAdress == dt1 ) {
            // my address
            delayMicroseconds(BEFORE_TRANSFER_PAUSE);
            digitalWrite(PIN_DE, HIGH);
            noInterrupts();
            Serial.write(hitForceValue);
            Serial.write((hitForceValue + dt1) ^ 0xa3);
            interrupts();
            mode=2;
          }
          else {
            // not my address
            prevTime= TCNT0;
            mode=3;
          }
        }
        else {
          // error
          mode=50;
        }
      }
      // check timeout
      relTime= TCNT0;
      if( relTime >= prevTime ) relTime -= prevTime;
      else relTime = (byte)255 - prevTime + relTime;     
      if( relTime > (byte)TIME_OUT_EXCH ) {
        // timeout
        mode=50;
      }      
      break;

    case 2:
      //------------------ read kick value, wait end of transfer
      if( (UCSR0A & 64) != 0 ) { 
        delayMicroseconds(AFTER_TRANSFER_PAUSE);
        digitalWrite(PIN_DE, LOW); // transmitter off  
        // clear
        while (1) { if (Serial.read() == 0xffff) break;}
        mode=0;
      }
      break;

    case 3:
      //------------------ read kick value, not my address
      if( Serial.available() != 0 ) {
        Serial.read();
        prevTime= TCNT0;
        mode=4;
      }        
      // check time out
      relTime= TCNT0;
      if( relTime >= prevTime ) relTime -= prevTime;
      else relTime = (byte)255 - prevTime + relTime;     
      if( relTime > (byte)TIME_OUT_EXCH ) {
        // тайм-аут
        mode=50;
      }      
      break;

    case 4:
      //------------------ read kick value, not my address
      if( Serial.available() != 0 ) {
        Serial.read();
        // clear
        while (1) { if (Serial.read() == 0xffff) break;}
        mode=0;
      }        
      // check time out
      relTime= TCNT0;
      if( relTime >= prevTime ) relTime -= prevTime;
      else relTime = (byte)255 - prevTime + relTime;     
      if( relTime > (byte)TIME_OUT_EXCH ) {
        // тайм-аут
        mode=50;
      }      
      break;

    case 5:
      //------------------  RGB, recive R bite
      if( Serial.available() != 0 ) {
        prevTime= TCNT0;
        dt2= Serial.read();        
        mode=6;
      }          
      // check time out
      relTime= TCNT0;
      if( relTime >= prevTime ) relTime -= prevTime;
      else relTime = (byte)255 - prevTime + relTime;     
      if( relTime > (byte)TIME_OUT_EXCH ) {
        // time out
        mode=50;
      }      
      break;

    case 6:
      //------------------  RGB, receive G bite
      if( Serial.available() != 0 ) {
        prevTime= TCNT0;
        dt3= Serial.read();        
        mode=7;
      }          
      // check time out
      relTime= TCNT0;
      if( relTime >= prevTime ) relTime -= prevTime;
      else relTime = (byte)255 - prevTime + relTime;     
      if( relTime > (byte)TIME_OUT_EXCH ) {
        // тайм-аут
        mode=50;
      }      
      break;

    case 7:
      //------------------ RGB, receive B bite
      if( Serial.available() != 0 ) {
        prevTime= TCNT0;
        dt4= Serial.read();        
        mode=8;
      }          
      // check time out
      relTime= TCNT0;
      if( relTime >= prevTime ) relTime -= prevTime;
      else relTime = (byte)255 - prevTime + relTime;     
      if( relTime > (byte)TIME_OUT_EXCH ) {
        // time out
        mode=50;
      }      
      break;

    case 8:
      //------------------  RGB, receive КК
      if( Serial.available() != 0 ) {
        dt5= Serial.read();        
        if( dt5 == ((byte)((dt1 + dt2 + dt3 + dt4) ^ 0xe5)) ) {
          // right
          // check address
          if( shieldAdress == (dt1 & 0x3f) ) {
            // my address
            delayMicroseconds(BEFORE_TRANSFER_PAUSE);
            digitalWrite(PIN_DE, HIGH);
            // load RGB
            analogWrite(PIN_RED_LED, dt2);
            analogWrite(PIN_GREEN_LED, dt3);
            analogWrite(PIN_BLUE_LED, dt4);
            Serial.write((dt1 + dt2 + dt3 + dt4) ^ 0xa3);
            mode=2;
          }
          else {
            // not my address
            prevTime= TCNT0;
            mode=4;
          }
        }
        else {
          // error
          mode=50;
        }
      }
      // check time out
      relTime= TCNT0;
      if( relTime >= prevTime ) relTime -= prevTime;
      else relTime = (byte)255 - prevTime + relTime;     
      if( relTime > (byte)TIME_OUT_EXCH ) {
        // time out
        mode=50;
      }      
      break;

    case 9:
      //------------------ reset kick value, receive bite 2
      if( Serial.available() != 0 ) {
        dt2= Serial.read();
        if( dt2 == (dt1 ^ 0xe5) ) {
          // right
          // check address
          if( shieldAdress == (dt1 & 0x3f)) {
            // my address
            delayMicroseconds(BEFORE_TRANSFER_PAUSE);
            digitalWrite(PIN_DE, HIGH);
            hitForceValue=0;            
            Serial.write(dt1 ^ 0xa3);
            mode=2;
          }
          else {
            // not my address
            prevTime= TCNT0;
            mode=4;
          }
        }
        else {
          // error
          mode=50;
        }
      }
      // check time out
      relTime= TCNT0;
      if( relTime >= prevTime ) relTime -= prevTime;
      else relTime = (byte)255 - prevTime + relTime;     
      if( relTime > (byte)TIME_OUT_EXCH ) {
        // time out
        mode=50;
      }      
      break;



    case 10:
      //------------------ enhanced, receive bite 2
      if( Serial.available() != 0 ) {
        prevTime= TCNT0;
        dt2= Serial.read();    
        if( dt1 == 0xc0 ) {
          // reset adresses
          if( dt2 == (dt1 ^ 0xe5) ) {
            // right
            shieldAdress= 51;
            digitalWrite(PIN_BLOCK_OUT, LOW);
            analogWrite(PIN_RED_LED, 128);
            analogWrite(PIN_GREEN_LED, 128);
            analogWrite(PIN_BLUE_LED, 128);
            mode=50;            
          }
          else {
            // error
            mode=50;
          }
        }
        else {
          // device address
          mode=11;            
        }
      }          
      // check time out
      relTime= TCNT0;
      if( relTime >= prevTime ) relTime -= prevTime;
      else relTime = (byte)255 - prevTime + relTime;     
      if( relTime > (byte)TIME_OUT_EXCH ) {
        // time out
        mode=50;
      }      
      break;


    case 11:
      //------------------ set address, receive КК
      if( Serial.available() != 0 ) {
        dt3= Serial.read();                
        if( dt3 == ((byte)((dt1 + dt2) ^ 0xe5)) ) {
          // right
          // check address
          if( shieldAdress == 51 ) {
            // my adress
            if( flagBlockLow == true ) {
              // block
              prevTime= TCNT0;
              mode=4;
            }
            else {
              // not block
              delayMicroseconds(BEFORE_TRANSFER_PAUSE);
              digitalWrite(PIN_DE, HIGH);            
              // load adress
              shieldAdress= dt2 & 0x3f;     
              digitalWrite(PIN_BLOCK_OUT, HIGH);       
              analogWrite(PIN_RED_LED, 0);
              analogWrite(PIN_GREEN_LED, 0);
              analogWrite(PIN_BLUE_LED, 0);
              Serial.write((dt1 + dt2) ^ 0xa3);            
              mode=2;
            }
          }
          else {
            // not my address
            prevTime= TCNT0;
            mode=4;
          }
        }
        else {
          // error
          mode=50;
        }
      }
      // check time out
      relTime= TCNT0;
      if( relTime >= prevTime ) relTime -= prevTime;
      else relTime = (byte)255 - prevTime + relTime;     
      if( relTime > (byte)TIME_OUT_EXCH ) {
        // time out
        mode=50;
      }      
      break;


    case 50:
      //------------------ receive error
      // clear
      while (1) { if (Serial.read() == 0xffff) break;}
      mode=0;
      break;
      

    default:
      //------------------ step error
      // clear
      while (1) { if (Serial.read() == 0xffff) break;}
      mode=0;
      break;
  }
  
}

//-------------------------------------- interrupt 200 мкс
void  timerInterrupt() {

  hForceValue= (byte)(BackgroundADC.analogRead() >> 2);
  if( hForceValue > hitForceValue) hitForceValue=hForceValue;
  BackgroundADC.analogStart(A0);

  // filtr block in  
  if( (PIND & (byte)0x80) == 0 ) {
    if( flagBlockLow == true ) {
      // 0 not 0
      if( blockCounter != 0 ) blockCounter--;    
    }
    else {
      // 0, old 1
      blockCounter++;
      if( blockCounter > (byte)FILTER_BLOCK_TIME ) {
        flagBlockLow= true;
        blockCounter=0;
      }
    }
  }
  else {
    if( flagBlockLow == false ) {
      // 1 old 1
      if( blockCounter != 0 ) blockCounter--;              
    }
    else {
      // 1, old 0
      blockCounter++;
      if( blockCounter > (byte)FILTER_BLOCK_TIME ) {
        flagBlockLow= false;
        blockCounter=0;
      }      
    }
  }
  
}
[/details]

I think that the mounting of the sensor is important. Does it react on pressure or strain...

this is so, options were tested for direct gluing to the shield, gluing to the console, which is attached to the shield with one end, and with two ends as well, fastening through a variety of gaskets. but as the experiment with the test board and the electromagnet showed, this is not exactly the case.

A high-pass filter passes high frequencies and blocks low frequencies. :wink:

You might try a larger capacitor for your peak detector to hold the peak longer. You can only read once every time through the loop and maybe the peak is too fast or the loop is too slow. But, that also means the capacitor won't charge-up as high. (An active peak detector would probably work better.)

I was going to use this scheme to set the average values of the signals from the sensors, at the same time remove the negative component of the signal and crush the high frequencies. do you think it's worth adding an active peak detector here?

One would think that repeating an action the same every time should give the same output. However in my experience with Piezo that is not the case.

What I think you are doing is:

Not sure if this is much help.....

I suggest:

  1. on one of the Plates Serial out the Peak read by the Arduino. Repeat the same test a number of times to see the variability of the result.

  2. See if you can sync two of the Arduinos. Then see if the time variation is measurable with your setup. I would think the only delay you could measure in this manner results from the rubber mounting washers.

Piezo sensors are terrible for absolute energy (i.e. voltage as you are measuring it) conversion. The connection of the Piezo to the plate is critical. Where on the plate is critical etc.

Not sure why you use a hardware peak detector circuit.
Any Arduino is fast/capable enough to process peak detecting of a piezo in software.
Example:

  piezoValue = 0; // reset
  for (int x = 0; x < 250; x++) { // multiple A/D readings
    rawValue = analogRead(piezoPin);
    if (rawValue > piezoValue) {
      piezoValue = rawValue; // store peaks
    }
  }

Leo..

everything is fine with the shields, the connection underestimates the impact force by more than 40%, I checked with smartphones :slight_smile: The problem is that I can't achieve repeatability from one sensor with obviously identical impacts (it's a pity that I can't upload a video here). I understand that the mounting of different sensors is not ideal and the signal level from them will be different, it is not difficult to solve with a tuning resistor, but why there is no stability from one, I cannot understand.

because the piezo sensor outputs an AC signal and needs to be rectified with a diode, arduino cannot measure negative values. also, the signal is very short and the time of voltage measurements does not necessarily have to be at the maximum part of the amplitude, so we put a capacitor. at the very beginning I tried without it, the result is terrible.

True, but you can bias the sensor mid-voltage (virtual ground).

First peak when hitting a piezo should be positive, so you can ignore the negative part.
Unfortunately you must select the piezos for that. The peak could be positive or negative, depending which way up the piezo has been glued onto the brass base.

A/D measuring time (0.1ms) is much faster than the piezo (without cap).
But A/D should still measure continuously to not miss the peak.
Data exchange can be done after the peak.
Leo..

I use interrupts, but I do not exclude the possibility of an error, if it is not difficult for you, I will be grateful for checking my code

Why?
I don't see the need for something so slow.

You should always post your code.
Use code tags (read the how to post sticky).
Leo..

my fault, now used the correct tag.

the fact is that I am not a professional in using arduino, that's why I asked for help, how do you recommend doing?

I would not be so sure about that!

The complaint regarding "I can't achieve repeatability from one sensor with obviously identical impacts" is rather spurious.

It should be clear that in this respect, impacts are never even remotely identical. There will always be significant variations in the impact and the resonances of the piezos - in the upper kilohertz range - will cause major variations in the waveform, so successive polls would need to occur within a few microseconds and that is just not practical in conjunction with performing other tasks.

Not that interrupt will necessarily be more effective; this is very much a mechanical problem. :thinking:

A scope will reveal more. Once the curves and variations are known a setup and strategy for data acquisition can be chosen.

Exactly.
I also expect a voltage divider could be needed for expected impacts (tens or hundreds of volt).
Say 1Megohm to piezo(+) and 10k to 100k to piezo brass,
both mounted very close to, or on the piezo,
With the joint (and ground/brass) going to the Arduino with short-ish shielded (audio) wire.
With the piezo selected for positive going impact (with a scope).
Leo..

How do you know the electromagnet is consistent?
It would be better to mount the sensor horizontal and drop a small mass on it always from the same height, that would ensure a more consistent test.

Tom... :grinning: :+1: :coffee: :australia:

I still believe my suggestion would provide useful information.

That is; Code one Arduino to output the peak voltage you are reading to output the raw A/D counts to the serial monitor. Repeat the impacts for 10+ times and look at the counts.
If nothing else this will show the range of the inconsistency.

And.... I don't believe the impacts are identical, small variations could easily cause your data being different.

I selected resistors and capacitors, at the moment I have achieved a spread of parameters of about 20 percent. the twisted pair showed a better result than the shielded wire. I think it is necessary to increase the impact force, now I will print something on a 3d printer that will drop the load from the same height. It's a pity that there are no old mice left, they had great balls in them. :grinning:


Did you know that Alt-PrtSc captures the current window to the clipboard so you can paste it into an image manager (IrfanView probably the best. :+1: )?