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;


    // Slow the rate of data acquisition

    if (volts > 1.5){
      digitalWrite(ledPin, HIGH);
      digitalWrite(ledPin, LOW);
  • Is it going over 1V?
    – Majenko
    Commented Nov 28, 2016 at 19:53
  • Yes it is, the peak is around 1.2V on the oscilloscope.
    – EmilyF
    Commented Nov 28, 2016 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
    Commented Nov 28, 2016 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
    Commented Nov 28, 2016 at 20:46

2 Answers 2


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(){
  pinMode(13, OUTPUT);

void loop(){
  if(analogRead(A0)>204){ //at ~1V the ADC will read between 204 and 205

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
    Commented Nov 29, 2016 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 ? Commented Nov 29, 2016 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
    Commented Nov 29, 2016 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. Commented Nov 30, 2016 at 3:39

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.