Arduino Touch sensor, Ultrasonic sensor, Heartbeat sensor

Touch sensor

https://create.arduino.cc/projecthub/Arca_Ege/how-to-use-a-touch-sensor-81b7f4

The circuit from sensor > arduino
VCC Pin > 5V
GND > GND
OUT > Pin 2
LED pin (short leg (anode) to ground, long leg to Pin 13)

int in = 2; 
int out = 13;  
int state = HIGH;  
int r;           
int p = LOW;    
long time = 0;       
long debounce = 200;
   
void setup()
{
  pinMode(in, INPUT);
  pinMode(out, OUTPUT);
}
void loop()
{
  r = digitalRead(in);
  if (r == HIGH && p == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else 
      state = HIGH;
    time = millis();    
  }
  digitalWrite(out, state);
  p = r;
}

Ultrasonic sensor

https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/
https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/

The circuit from sensor > arduino
+5 > 5V
Trigger > pin 7
Echo > pin 6
GND > GND

const int trigPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6; // Echo Pin of Ultrasonic Sensor

void setup() {
   Serial.begin(9600); // Starting Serial Terminal
}

void loop() {
   long duration, inches, cm;
   pinMode(trigPin, OUTPUT);
   digitalWrite(trigPin, LOW);
   delayMicroseconds(2);
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(trigPin, LOW);
   pinMode(echoPin, INPUT);
   duration = pulseIn(echoPin, HIGH);
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);
   Serial.print(inches);
   Serial.print("in, ");
   Serial.print(cm);
   Serial.print("cm");
   Serial.println();
   delay(100);
}

long microsecondsToInches(long microseconds) {
   return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
   return microseconds / 29 / 2;
}

Pulse/ Heartbeat Sensor

https://www.hackster.io/Johan_Ha/from-ky-039-to-heart-rate-0abfca — The author converted the signal from the sensor into human-readable beats per minute.

The circuit from sensor > arduino
Signal (S pin) > A0
5V ( + middle pin) > 5V
GND (- pin) > GND

Note: place your finger between the IR led and the light transistor of the sensor. Make sure they are contain in a dark environment to remove noise reflecting from artificial lights. (The reason why the SpO2 probe have opaque protective covers?)

#define samp_siz 4
#define rise_threshold 4

// Pulse Monitor Test Script
int sensorPin = 0;

void setup() {
    Serial.begin(9600);
}

void loop ()
{
    float reads[samp_siz], sum;
    long int now, ptr;
    float last, reader, start;
    float first, second, third, before, print_value;
    bool rising;
    int rise_count;
    int n;
    long int last_beat;

    for (int i = 0; i < samp_siz; i++)
      reads[i] = 0;
    sum = 0;
    ptr = 0;

    while(1)
    {
      // calculate an average of the sensor
      // during a 20 ms period (this will eliminate
      // the 50 Hz noise caused by electric light
      n = 0;
      start = millis();
      reader = 0.;
      do
      {
        reader += analogRead (sensorPin);
        n++;
        now = millis();
      }
      while (now < start + 20);  
      reader /= n;  // we got an average
      
      // Add the newest measurement to an array
      // and subtract the oldest measurement from the array
      // to maintain a sum of last measurements
      sum -= reads[ptr];
      sum += reader;
      reads[ptr] = reader;
      last = sum / samp_siz;
      // now last holds the average of the values in the array

      // check for a rising curve (= a heart beat)
      if (last > before)
      {
        rise_count++;
        if (!rising && rise_count > rise_threshold)
        {
          // Ok, we have detected a rising curve, which implies a heartbeat.
          // Record the time since last beat, keep track of the two previous
          // times (first, second, third) to get a weighed average.
          // The rising flag prevents us from detecting the same rise more than once.
          rising = true;
          first = millis() - last_beat;
          last_beat = millis();

          // Calculate the weighed average of heartbeat rate
          // according to the three last beats
          print_value = 60000. / (0.4 * first + 0.3 * second + 0.3 * third);
          Serial.print(print_value);
          Serial.print('\n');
          third = second;
          second = first;
        }
      }
      else
      {
        // Ok, the curve is falling
        rising = false;
        rise_count = 0;
      }
      before = last;
      ptr++;
      ptr %= samp_siz;
    }
}

Leave a comment

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