Analog Input (multiple buttons) not at 0 unless USB is plugged in

Hope this is the right forum section!

So I made a multiple button analog input using this guide:
https://rayshobby.net/wordpress/multiple-button-inputs-using-arduino-analog-pin/

I am using a proMicro as my controller to run some LEDs. When the USB cable is plugged in and attached to the PC, everything works fine. However, when I unplug the USB cable, it seems my analog '0' value is more around 80-100. Can't verify easily as to monitor serial I have to plug in the USB cable. So when I am not pressing any buttons it fires off the input command looking for >60.

I have double and triple checked that I have good grounds. Everything shows proper continuity.
I am using a good 5V power supply, and have it plugged in even with the USB connected so as to power the LEDs.

I have checked my wiring many times. It works fine with the USB, and fine without if I hold down the button (lock it into the specific IF THEN state), but when I let go it fires off a step that shouldn't turn on.

It also seems that when I press the button, and with no USB cable, it doesn't immediately jump to the correct analog value but instead works up to it (will fire off the wrong section before the correct one).

I am connected to Vcc, Gnd (common throughout all components), and an analog pin.

And to reiterate, this works fine when I have the USB cable plugged in and connected to my PC, even if the IDE isn't open. Plugging in or pulling out the USB cable will immediately swap it between functioning correctly and not.

/*
Momentary potentiometer (onl gives Analog value while button held)
feeds into the RemotePin, read during the Void loop to determine
what color to turn on all the LED's. A Delay is placed to avoid any
readings while the button is released.
No button press is an Analog value of 0 (zero)
*/

#include <FastLED.h>

String Effect; //change current effect

#define NUM_LEDS 60

#define RemotePin 9
#define LED_PIN 10

CRGB led[NUM_LEDS];


void setup() {
  Serial.begin(9600);
  pinMode(RemotePin, INPUT);

  
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(led, NUM_LEDS);
  delay (3000);
  setAll(0,0,0);
  FastLED.show();
}

//---------main program

void loop() {

  if ( analogRead(RemotePin) > 900 ){
    Effect = "OFF";
    setAll(0,0,0);
    delay(500);
  }  
  else if ( analogRead(RemotePin) > 800 ){
    Effect = "Red";
    setAll(255,0,0);
    delay(500);
  }
  else if ( analogRead(RemotePin) > 700 ){
    Effect = "Green";
    setAll(0,255,0);
    delay(500);
  }
  else if ( analogRead(RemotePin) > 600 ){
    Effect = "Blue";
    setAll(0,0,255);
    delay(500);
  }
  else if ( analogRead(RemotePin) > 500 ){
    Effect = "Purple";
    setAll(127,0,255);
    delay(500);
  }
  else if ( analogRead(RemotePin) > 400 ){
    Effect = "Yellow";
    setAll(255,255,0);
    delay(500);
  }
  else if ( analogRead(RemotePin) > 300 ){
    Effect = "Teal";
    setAll(0,255,255);
    delay(500);
  }
  else if ( analogRead(RemotePin) > 200 ){
    Effect = "White";
    setAll(100,100,100);
    delay(500);
  } 
  else if ( analogRead(RemotePin) > 120 ){
    Effect = "Orange";
    setAll(255,128,0);
    delay(500);
    }
  else if ( analogRead(RemotePin) > 60 ){
    Effect = "Fuscia";
    setAll(255,0,255);
    delay(500);
    }
    
  Serial.println(Effect);
  Serial.println(analogRead(RemotePin));
  
delay(100);

}  //end main program


//--------set values for all LEDS----------------
void setAll(byte new_clr0,byte new_clr1,byte new_clr2){
  for (byte i = 0; i < NUM_LEDS; i++){
    led[i] = CRGB(new_clr0, new_clr1, new_clr2);
  }
  FastLED.show();
}

Any thoughts would greatly be appreciated!

Hi,
Can you post a picture of your project so we can see your component layout.
How do you know your analog value goes high when you unplug the USB?

When you have the USB connected, is it supplying the 5V for the ProMini or is the external 5V supply?
What is the external supply?
Have you measured the 5V at the ProMini when you have USB connected and disconnected?

Can you measure A0 pin with USB connected and disconnected please.

Thanks.. Tom.. :slight_smile:

That 1M resistor in the circuit is probably the problem, its far too high and allowing interference to break into the analog pin from stray electric fields.

I suspect your "a good 5V power supply," isn't grounded, so is injecting switch-mode noise into the circuit,
connecting the USB grounds the circuit and diverts this noise to ground.

The circuit is unfortunately poorly designed as its impedance is a whopping 1M when no buttons are pressed.
A better circuit would have lowish impedance no matter what the state of the buttons was.

You could try adding a 100nF or 10nF cap across the 1M resistor.

You have not indicated what the second power source is, the one that powers the arduino when the USB is not connected.

When the USB is not connected, what is the power source and where is it connected ?