Go Down

Topic: Preventing digital output disrupting analog input (Read 97 times) previous topic - next topic

hugoscott530

Apr 16, 2019, 12:57 pm Last Edit: Apr 17, 2019, 02:05 pm by hugoscott530
Hello,

I am using a pulse sensor (from pulsesensor.com) connected to A13 of an Arduino Mega. I am attempting to output a digital heartbeat clock signal to an LED and a mono jack socket whilst also calculating the heart rate (bpm). The mono jack is connected directly to pin32 and the LED is in series with a 100 Ohm resistor connected to pin23.

The clock signal goes HIGH when the pulse signal is more than the threshold value. I am controlling the threshold value with a slide potentiometer connected to A0. When I output the clock signal to just the mono jack socket, it works perfectly. However, when I output the clock signal to the mono jack socket and the LED, the pulse signal becomes very noisy when it passes the threshold value causing the clock output to glitch.

I have taken screenshots of the serial plotter with and without the LED output. The pulse signal and threshold value are being plotted. I have attached the images to this post.

The code I am using is below...

Code: [Select]
int HRTupperThreshold = 0;
int HRTlowerThreshold = 0;
int HRTsensorValue = 0;
int HRTsensorPin = A13;
int HRTbeatCLK = 32;
int HRTbeatCLKLED = 23;
float HRTbpm = 0.0;
bool IgnoreReading = false;
bool FirstPulseDetected = false;
unsigned long FirstPulseTime = 0;
unsigned long SecondPulseTime = 0;
unsigned long PulseInterval = 0;
const unsigned long delayTime = 10;
const unsigned long delayTime2 = 10;
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
int HRTthresholdPin = A0;
float HRTthresholdPotValue;

void setup() {
  Serial.begin(9600);
  pinMode(HRTbeatCLK, OUTPUT);
  pinMode(HRTbeatCLKLED, OUTPUT);
  digitalWrite(HRTbeatCLKLED, LOW);
}

void loop() {
  HRTthresholdPotValue = analogRead(HRTthresholdPin);
  HRTupperThreshold = HRTthresholdPotValue;
  HRTlowerThreshold = HRTthresholdPotValue;
 
  unsigned long currentMillis = millis();
  if(myTimer1(delayTime, currentMillis) == 1){
    HRTsensorValue = analogRead(HRTsensorPin);
    if(HRTsensorValue > HRTupperThreshold && IgnoreReading == false){
      if(FirstPulseDetected == false){
        FirstPulseTime = millis();
        FirstPulseDetected = true;
      }
      else{
        SecondPulseTime = millis();
        PulseInterval = SecondPulseTime - FirstPulseTime;
        FirstPulseTime = SecondPulseTime;
      }
      IgnoreReading = true;
      digitalWrite(HRTbeatCLK, HIGH);
      digitalWrite(HRTbeatCLKLED, HIGH);
    }
    if(HRTsensorValue < HRTlowerThreshold && IgnoreReading == true){
      IgnoreReading = false;
      digitalWrite(HRTbeatCLK, LOW);
      digitalWrite(HRTbeatCLKLED, LOW);
    } 
    HRTbpm = (1.0/PulseInterval) * 60.0 * 1000;
  }
  if(myTimer2(delayTime2, currentMillis) == 1){

    Serial.print("\t");
    Serial.print(HRTsensorValue);
    Serial.print("\t");
    Serial.println(HRTthresholdPotValue);
  }
  }
  int myTimer1(long delayTime, long currentMillis){
    if(currentMillis - previousMillis >= delayTime){previousMillis = currentMillis;return 1;}
    else{return 0;}
  }
  int myTimer2(long delayTime2, long currentMillis){
    if(currentMillis - previousMillis2 >= delayTime2){previousMillis2 = currentMillis;return 1;}
    else{return 0;}
  }


Is it possible to prevent the LED disrupting the pulse sensor signal? Your help will be much appreciated, let me know if you need anymore information.

Many thanks!

pylon


wvmarle

When I output the clock signal to just the mono jack socket, it works perfectly. However, when I output the clock signal to the mono jack socket and the LED, the pulse signal becomes very noisy when it passes the threshold value causing the clock output to glitch.
So if I understand this correctly (the lack of schematics is NOT helping) you're trying to power an LED directly off a sensor output?

That may very well be your problem. Add a buffer (unity gain opamp or comparator) capable of driving that LED.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Go Up