2

I'm trying to read in an analog signal (a heartbeat) to pin A0 on the Arduino Uno and then convert it to digital. I'm hoping to use the digital signal to make an LED light up every time the voltage of the heartbeat signal goes over 1 volt. This is the code I have right now, but something is wrong because it isn't lighting up the LED.

unsigned int analog0;             // Raw data from A0 ranging from 0 to 1023
unsigned long data_range = 2000;  // Length of time to record data for
double volts;                     // The analog data from A0 converted to volts
int ledPin = 13;
unsigned long start_time;
unsigned long data_time;

void setup() {
  Serial.begin(9600);             // Initialize and set serial port baud rate
  pinMode(ledPin , OUTPUT);
}

void loop() {
  start_time = millis();    
  while ((millis() - start_time) < data_range)
  {
    // Record current time of analog acquisition
    data_time = millis();

    // Record analog data on A0
    analog0 = analogRead(0);

    // Convert raw data into voltage
    volts = (analog0 / 1023.0) * 5;

    Serial.print(volts);
    Serial.println();

    // Slow the rate of data acquisition
    //delay(1);

    if (volts > 1.5){
      digitalWrite(ledPin, HIGH);
      //delay(50);
    }
    else{
      digitalWrite(ledPin, LOW);
    }
  }     
}
  • Is it going over 1V? – Majenko Nov 28 '16 at 19:53
  • Yes it is, the peak is around 1.2V on the oscilloscope. – EmilyF Nov 28 '16 at 20:42
  • 1
    I mean does the Arduino think it is going over 1V? And if the peak is 1.2V and you want to know if it is over 1V, why are you looking to see if it is over 1.5V? – Majenko Nov 28 '16 at 20:44
  • We were just trying arbitrary values because nothing below that seemed to work, and whatever value we put there affects the voltage printed out to the serial monitor for some reason. – EmilyF Nov 28 '16 at 20:46
1

Try commenting out the serial in your sketch. In my experience serial communication takes time, and I assume you have a short window where the voltage peaks at more than a volt. Or initialize the serial with a stupidly high baud rate.
Edit: even better, you could try this for a faster response time:

void setup(){
  digitalWrite(13,0);
  pinMode(13, OUTPUT);
}

void loop(){
  if(analogRead(A0)>204){ //at ~1V the ADC will read between 204 and 205
    digitalWrite(13,1);
    delay(100);
    digitalWrite(13,0);
  }
}
-1

I think your voltage formula is wrong. Try this:

float volts = analog0 * (5.0 / 1023.0);

  • This it the same formula, just written differently. – gre_gor Nov 29 '16 at 15:09
  • @emilyf dis you try printing the raw value of analog0 ? Also maybe try a different analog input in case of A0 being bad ? – Andre Courchesne Nov 29 '16 at 16:17
  • We did try to print the raw value of analog0 and it was jumping from 0 to 5 within a second. We figured out how to make the LED light up using a comparator op amp configuration by comparing our heartbeat signal to a 0.8V signal, and when the heartbeat signal was above 0.8 our LED lights up. Unfortunately we couldn't do it with the Arduino, but any suggestions about how to fix the code are welcome! – EmilyF Nov 29 '16 at 16:47
  • @emilyf just tried your code on an Arduino UNO exactly as you have put it and it works as expected. My theory is either a blown ADC pin on your Arduino board or a miswiring. – Andre Courchesne Nov 30 '16 at 3:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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