Analog read - or a tale of two sketches

My project reads the A0 input generated by a number of buttons wired in series with stepped resistors. That part works fine as, depending on which button is pressed, I get varied inputs. The issue I'm presently trying to deal with is why the A0 reading doesn't revert back to (or close to) zero after the button is released.

I tested the functionality using this sketch:


int buttonInput = 0;


void setup(){

  pinMode(A0, INPUT);
 
  Serial.begin(9600);
}
 
void loop(){ 
 
  buttonInput=analogRead(A0);
  Serial.print("A0 reading:");
  Serial.println(buttonInput);

  delay(2000);    
}

With this result:

When the button is pressed the reading jumps to 272 and then immediately drops back to 3 or 4 when it's released. This is the behaviour I expect to see.

However, when I insert the same A0 read functionality into this sketch:


#include <SD.h>                      // SD library
#define SD_ChipSelectPin 10  //pin connected to CS pin on SD card.
/* Other SD card pin connections:
 *  CS  - 10
 *  SCK - 13
 *  Miso  - 12
 *  MOSi  - 11https://support.arduino.cc/hc/en-us/sections/360003198300
 */
#include <TMRpcm.h>          //  also need to include this library...
#include <SPI.h>

TMRpcm tmrpcm;   // create an object for use in this sketch
const int greenLEDPin = 2;    // LED connected to digital pin 2
const int redLEDPin = 3;     // LED connected to digital pin 3
int buttonInput = 0; // current analog value read from pin A0
int buttonNumber = 0; // which one of the 7 selector buttons activated bnased on buttonInput value
int tuneSelection = 0; // which set of songs/tunes selected based on reading of pin A1
int tuneInput = 0; // analog read from pin A1

void setup(){
/*  Set the volume and check that the SD card is installed and operational.
 *  Set red LED flashing if not, else turn on green LED.
 * 
 */
// set the analog pin as input

  pinMode (A0, INPUT);
  
//set the digital pins as outputs

  pinMode(greenLEDPin, OUTPUT);
  pinMode(redLEDPin, OUTPUT);
  tmrpcm.setVolume (6);
  tmrpcm.speakerPin = 9; // speaker output pin
  
  Serial.begin(9600);
  while (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized:
    // If card not ready, flash red pin
    Serial.println("SD fail"); 
    digitalWrite(redLEDPin, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay (1000);                // wait for a second
    digitalWrite(redLEDPin, LOW);    // turn the LED off by making the voltage LOW
                        // wait for a second
    
  }
  Serial.println("SD read OK");
  digitalWrite(redLEDPin, LOW);
  digitalWrite(greenLEDPin, HIGH);
  delay(500);
  }



void loop(){  

  /*tuneSelection = analogRead (A1); // determine which selection of tunes/sounds based on pot position

 if (tuneInput >= 0 && tuneInput <= 200)
  {tuneSelection = 1;
  }
  if (tuneInput >= 201 && tuneInput <= 400)
  {tuneSelection = 2;
  }
  */
  tuneSelection = 1;
  buttonInput = analogRead(A0);  //Check if a button has been pressed

  Serial.print("A0 reading: ");
  Serial.println(buttonInput);

  if (tuneSelection = 1)
  {
    if (buttonInput >= 30 && buttonInput <= 110) 
    {
      tmrpcm.play ("Raffi1.wav");
      while (tmrpcm.isPlaying ())
      {
        digitalWrite(redLEDPin, HIGH);
        digitalWrite(greenLEDPin, LOW);
        
        delay (5000);}
    }

    if (buttonInput >= 111 && buttonInput <= 175) 
    {
      tmrpcm.play ("Raffi2.wav");
      while (tmrpcm.isPlaying ())
      {
        digitalWrite(redLEDPin, HIGH);
        digitalWrite(greenLEDPin, LOW);
        
        delay (5000);}
    }
  }
      digitalWrite(redLEDPin, LOW);
      digitalWrite(greenLEDPin, HIGH);
      
      while (buttonInput>=30)
      {
        buttonInput=analogRead(A0);
        Serial.print("A0 reading: ");
        Serial.println(buttonInput);
        delay(3000);
      }
 
}

I get this result:

Screen Shot 2022-01-22 at 10.31.33 AM

A button press generates a reading of 352 but, when released, the A0 reading only drops to the 120 range, which creates a problem in my code.

Can anyone explain what's happening here and/or what I've done wrong?

Thanks.

Hello
Post schematic and sketch.

You probably do not have the button switch pulled up or down when the switch is open. But that is a guess without seeing a schematic.

Here's a diagram of the setup. Note that it's identical for both sketches shown above.

It may well be a wiring issue - if there is a volt drop along the Ov or 5v line due to speaker current for example

Take a separate wire to your resistor chain and back to 0v from any other wires .
If you have them spare , use digital inputs , a better solution .

Your amplifier should be powered separately not from the 5 v pin

Thanks for that. I'll make the change and see if that makes a difference.

I decided to use the analog input with chained resistors because, eventually, I'll be having more inputs (buttons) and didn't want to use up all the pins. This seemed more flexible, but perhaps not.

What's confusing me is that the physical setup is identical in both cases, so I would assume it would function identically. Not so, it seems.

Your resistor values are a bit low too , I would go for something in the 1k region - the push button resistance may be significant ?

This changed things considerably. By putting the amplifier/speaker on a separate power source the A0 voltages dropped to near-zero, as expected. Obviously having them on the same power source was creating some sort of cross-over. Wish I knew why, but this will work for now. Thanks.

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