Problem reading pin state

Hello.

I’m trying to see if I can use the pins on the Grove to run external circuits, like push buttons and toggle switches that do not connect directly to one of the 4-pin Grove ports. Specifically, I’m trying to use the side row digital pins to detect the state of a switch and then do something, in this example - change the color of an LED (which is connected to a grove port).

I’m trying to replicate this schematic:
orinsss.jpg

This is my setup:
20160515_124531.jpg

Here’s the code:

#include <ChainableLED.h>
#define NUM_LEDS 1
ChainableLED leds(6, 7, NUM_LEDS);

void setup() {
  pinMode(2, INPUT);
}

void loop() {
  int p=digitalRead(2);  
  if(p==HIGH){
    leds.setColorRGB(0,0,255,0);
  }
  else{
    leds.setColorRGB(0,255,0,0);
  }
    
  delay(100);
}

I’ve tried using 220ohm 440ohm, 1000ohm resistors. If I connect the setup to the ground as shown in the schematic, upon switch ON, the device shuts down - I’m guessing it’s tripping the internal safeguard because the current is too high? If I don’t use the ground, the pin state just doesn’t get detected.

What am I missing? Why doesn’t this work?

Hello,

Can you please modify your hardware connection as shown in the attached picture

Let us know the results.

Thanks and Regards
Connection_Switch.png

This worked! Thank you. Could you explain why the other way did not work?

Sorry for the delayed response, I had trouble logging in - in fact, still cannot login using Chrome. Finally tried with IE, and it let me get in…

Hi,

Thanks for letting us know it worked.

In your previous circuit you was actually trying to short the Vcc and GND using the switch so only your board got tripped.

Regards,
kavi