Trying to work with 3 analog sensors in one Arduino

Hi, I am trying to use three different sensors + a LCD screen for a project related to health (all in one Arduino UNO). All the sensors I am trying to use have analog outputs and are:

  • Temperature (LM35)
  • Pulse sensor
  • Piezoelectric sensor

Right now all of these sensors and the LCD that show their values work properly by their own. But when I try to combine them in order to get the results at the same time, the temperature sensor goes crazy (its input starts to fluctuate a lot). I thought it might be related to the power supply, as the temperature sensor is the only one that needs a higher current, but I connected an external power supply module and it still does not work. I am using the following code, but I am not sure if it can be that there is an error in the code that is preventing the sensors to be read at the same time.

#include <LiquidCrystal.h>
LiquidCrystal rodolfo(3,4,10,11,12,13); // (rs, enable, d4,d5,d6,d7) 

#define USE_ARDUINO_INTERRUPTS true    // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h>     // Includes the PulseSensorPlayground Library.   

//  Variables
int golpes=0;
int impacto;
float volts;
float celsius;
const int PulseWire = 2;       // PulseSensor PURPLE WIRE connected to ANALOG PIN 2
int Threshold = 550;           // Determine which Signal to "count as a beat" and which to ignore.
                               
PulseSensorPlayground pulseSensor;  // Creates an instance of the PulseSensorPlayground object called "pulseSensor"

void setup() {   

  Serial.begin(9600);          // For Serial Monitor

  // Configure the PulseSensor object, by assigning our variables to it. 
  pulseSensor.analogInput(PulseWire);   
  pulseSensor.setThreshold(Threshold);   

  // Double-check the "pulseSensor" object was created and "began" seeing a signal. 
   if (pulseSensor.begin()) {
    Serial.println("We created a pulseSensor Object !");  //This prints one time at Arduino power-up,  or on Arduino reset.  
  }
  rodolfo.begin(16,2);
  rodolfo.clear();
  rodolfo.setCursor(0,0);
  rodolfo.print("BPM:");
  rodolfo.setCursor(6,0);
  rodolfo.print("Hits:");
  rodolfo.setCursor(12,0);
  rodolfo.print("Temp:");
}

void loop() {

pulso();
temp();
piezo();
  delay(500);                    

}


void pulso(){
  int myBPM = pulseSensor.getBeatsPerMinute();  // Calls function on our pulseSensor object that returns BPM as an "int".
                                               // "myBPM" hold this BPM value now. 

 if (pulseSensor.sawStartOfBeat()) {            // Constantly test to see if "a beat happened". 
 //Serial.println("♥  A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened".
 //Serial.print("BPM: ");                        // Print phrase "BPM: " 
 //Serial.println(myBPM);                        // Print the value inside of myBPM. 
 rodolfo.setCursor(0,1);
 rodolfo.print(myBPM);
 rodolfo.print("   ");
}
  }

void temp(){
  int value=analogRead(A1);
  float volts=(value*5)/1023.0;
  float celsius=volts*100;
 // Serial.print(value);
  //Serial.println(" C");
  rodolfo.setCursor(11,1);
  rodolfo.print(String(celsius));
}

void piezo(){
  impacto=analogRead(A0);
  Serial.println(impacto);
  if (impacto<600){
    golpes++;
    Serial.println(golpes);
    delay(500);
  }
  rodolfo.setCursor(7,1);
  rodolfo.print(golpes);
 }

Also, these are the connections I made, with the difference that I added a power supply module for the 5V output.

That's too bad. How can we help? We have almost no information about your project.

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966

I'm new in the Arduino world, so I am trying to understand if it is possible to connect those 3 sensors in one same Arduino (using different analog inputs) for reading all of the sensors at the same time. All the connections are correct, as all the sensors works properly by their own, but the problem start when I try to read their values at the same time. Right now, I am using an Arduino UNO with the following code (I do not know if there is a mistake in my code that is preventing the sensors to work at the same time, or if the problem is related to electrical components):

#include <LiquidCrystal.h>
LiquidCrystal rodolfo(3,4,10,11,12,13); // (rs, enable, d4,d5,d6,d7) 

#define USE_ARDUINO_INTERRUPTS true    // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h>     // Includes the PulseSensorPlayground Library.   

//  Variables
int golpes=0;
int impacto;
float volts;
float celsius;
const int PulseWire = 2;       // PulseSensor PURPLE WIRE connected to ANALOG PIN 2
int Threshold = 550;           // Determine which Signal to "count as a beat" and which to ignore.
                               
PulseSensorPlayground pulseSensor;  // Creates an instance of the PulseSensorPlayground object called "pulseSensor"

void setup() {   

  Serial.begin(9600);          // For Serial Monitor

  // Configure the PulseSensor object, by assigning our variables to it. 
  pulseSensor.analogInput(PulseWire);   
  pulseSensor.setThreshold(Threshold);   

  // Double-check the "pulseSensor" object was created and "began" seeing a signal. 
   if (pulseSensor.begin()) {
    Serial.println("We created a pulseSensor Object !");  //This prints one time at Arduino power-up,  or on Arduino reset.  
  }
  rodolfo.begin(16,2);
  rodolfo.clear();
  rodolfo.setCursor(0,0);
  rodolfo.print("BPM:");
  rodolfo.setCursor(6,0);
  rodolfo.print("Hits:");
  rodolfo.setCursor(12,0);
  rodolfo.print("Temp:");
}

void loop() {

pulso();
temp();
piezo();
  delay(500);                    

}


void pulso(){
  int myBPM = pulseSensor.getBeatsPerMinute();  // Calls function on our pulseSensor object that returns BPM as an "int".
                                               // "myBPM" hold this BPM value now. 

 if (pulseSensor.sawStartOfBeat()) {            // Constantly test to see if "a beat happened". 
 //Serial.println("♥  A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened".
 //Serial.print("BPM: ");                        // Print phrase "BPM: " 
 //Serial.println(myBPM);                        // Print the value inside of myBPM. 
 rodolfo.setCursor(0,1);
 rodolfo.print(myBPM);
 rodolfo.print("   ");
}
  }

void temp(){
  int value=analogRead(A1);
  float volts=(value*5)/1023.0;
  float celsius=volts*100;
 // Serial.print(value);
  //Serial.println(" C");
  rodolfo.setCursor(11,1);
  rodolfo.print(String(celsius));
}

void piezo(){
  impacto=analogRead(A0);
  Serial.println(impacto);
  if (impacto<600){
    golpes++;
    Serial.println(golpes);
    delay(500);
  }
  rodolfo.setCursor(7,1);
  rodolfo.print(golpes);
 }

Could be... but hard to tell as you haven't shown us how they are connected. A schematic would help.

I am using an Arduino UNO with the following code

void temp(){
  int value=analogRead(A7);

How are you accessing A7 on a UNO?

This is the way I connected the entire circuit. The only difference with this image is that I added a power supply module in order to give the voltage and current.

It was a typing mistake as I tried the code in a MEGA, thanks for noticing it

So that's an UNO.. and pin A7 doesn't exist on an UNO ?

and looks like you are connected to pin A1 ?

That's right, @cattledog also noticed it, but as I told him, it was a typing mistake as I tried the code in a MEGA. Thanks for the correction too.

So where's the picture of how it's all connected?

There is only one ADC on a uno which the analog input channels share.
Sometimes there can be residual voltage in the analog MUX when switching channels. Taking a first reading and discarding it before using the value of a second reading is a technique often used.

I made an edition to the original post and I added the picture.

of an UNO... but you say you're using a MEGA ?

Ah no, right now I am using the UNO (I only tried the code in the MEGA once)

I am sorry, I am new into this Arduino world. But does that mean that I should read each analog input one at a time (for example, read A0, discard its value --> read A1, discard its value)?

I'm not clear about the pulse sensor but try with your read functions starting like this

void piezo(){
  impacto=analogRead(A0);//read and don't use
  impacto=analogRead(A0);

void temp(){
  int value=analogRead(A1);//read and dont use
  int value=analogRead(A1);

void pulso(){
  analogRead(A2);//read and don't use
  int myBPM = pulseSensor.getBeatsPerMinute();
 
1 Like

It worked :grinning:, thanks a lot

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.