Skip to main content

Grove - Switch(P)

This Grove – Switch is a mini SPDT slide, great for “ON/OFF” situations. It is a reliable switch of great build quality that we adopt it on many of our boards. You should stock some for your Grove prototyping system.

What does “P” mean? “P” stands for “panel mount” in this product.

Version

Product VersionChangesReleased Date
Grove-Switch(P) V1.0InitialJul 2012

Features

  • Grove Interface
  • Easy to use
  • Basic Grove element
tip

More details about Grove modules please refer to Grove System

Specification

ParameterValue/Range
Operating voltage3.3/5V
Electrical Life10,000 cycles
Operation Force200 ± 50gf
Operation Temperature-20℃ to +80℃
Size20mmX20mm

Platforms Supported

ArduinoRaspberry Pi
caution

The platforms mentioned above as supported is/are an indication of the module's software or theoritical compatibility. We only provide software library or code examples for Arduino platform in most cases. It is not possible to provide software library / demo code for all possible MCU platforms. Hence, users have to write their own software library.

Getting Started

note

If this is the first time you work with Arduino, we firmly recommend you to see Getting Started with Arduino before the start.

Play With Arduino

Hardware

  • Step 1. Prepare the below stuffs:
Seeeduino V4.2Base ShieldGrove-Switch(P)Grove - Purple LED (3mm)
enter image description hereenter image description hereenter image description here
Get One NowGet One NowGet One NowGet One Now
  • Step 2. Connect Grove-Switch(P) to D2 port of Grove-Base Shield.
  • Step 3. Connect Grove-LED to D6 port of Grove-Base Shield.
  • Step 4. Plug Grove - Base Shield into Seeeduino.
  • Step 5. Connect Seeeduino to PC via a USB cable.

note

If we don't have Grove Base Shield, We also can directly connect Grove-Switch(P) and Grove - Purple LED (3mm) to Seeeduino as below.

SeeeduinoGrove-Switch(P)SeeeduinoGrove - Purple LED (3mm)
5VRed5VRed
GNDBlackGNDBlack
NCWhiteNCWhite
D2YellowD6Yellow

Software

  • Step 1. Please copy below code to Arduio IDE and upload to arduino. If you do not know how to upload the code, please check how to upload code.
const int switchPin = 2;     // the number of the pushbutton pin
const int ledPin = 6; // the number of the LED pin

int switchState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the switch pin as an input:
pinMode(switchPin, INPUT);
Serial.begin(9600);
}

void loop(){
// read the state of the switch value:
switchState = digitalRead(switchPin);

if (switchState == HIGH) {
//turn LED on:
digitalWrite(ledPin, HIGH);
Serial.println("switch high!");
}
else {
//turn LED off:
digitalWrite(ledPin, LOW);
Serial.println("switch low");
}
}

  • Step 2. When we switch to high and the LED will be on. We also can see the Serial output as below.
switch high!
switch high!
switch high!

Play With Raspberry Pi (With Grove Base Hat for Raspberry Pi)

Hardware

  • Step 1. Things used in this project:
Raspberry piGrove Base Hat for RasPiGrove - Switch P
enter image description hereenter image description hereenter image description here
Get ONE NowGet ONE NowGet ONE Now
  • Step 2. Plug the Grove Base Hat into Raspberry.
  • Step 3. Connect the Switch to port 12 of the Base Hat.
  • Step 4. Connect the Raspberry Pi to PC through USB cable.

note

For step 3 you are able to connect the switch to any GPIO Port but make sure you change the command with the corresponding port number.

Software

caution

If you are using Raspberry Pi with Raspberrypi OS >= Bullseye, you have to use this command line only with Python3.

  • Step 1. Follow Setting Software to configure the development environment.
  • Step 2. Download the source file by cloning the grove.py library.
cd ~
git clone https://github.com/Seeed-Studio/grove.py

  • Step 3. Excute below commands to run the code.
cd grove.py/grove
python3 grove_switch.py 12

Following is the grove_switch.py code.



import time
from grove.gpio import GPIO


class GroveTiltSwitch(GPIO):
def __init__(self, pin):
super(GroveTiltSwitch, self).__init__(pin, GPIO.IN)

@property
def state(self):
return super(GroveTiltSwitch, self).read()


Grove = GroveTiltSwitch


def main():
import sys

if len(sys.argv) < 2:
print('Usage: {} pin'.format(sys.argv[0]))
sys.exit(1)

swicth = GroveTiltSwitch(int(sys.argv[1]))


while True:
if swicth.state is 1:
print("on")
else:
print("off")
time.sleep(1)


if __name__ == '__main__':
main()


success
If everything goes well, you will be able to see the following result

pi@raspberrypi:~/grove.py/grove $ python3 grove_switch.py 12
off
off
on
off
on
on
off
^CTraceback (most recent call last):
File "grove_switch.py", line 70, in <module>
main()
File "grove_switch.py", line 66, in main
time.sleep(1)
KeyboardInterrupt


You can quit this program by simply press ++ctrl+c++.

Play With Raspberry Pi (with GrovePi_Plus)

Hardware

  • Step 1. Prepare the below stuffs:
Raspberry piGrovePi_PlusGrove-Switch(P)
enter image description hereenter image description hereenter image description here
Get One NowGet One NowGet One Now
  • Step 2. Plug the GrovePi_Plus into Raspberry.
  • Step 3. Connect Grove-Switch(P) to D3 port of GrovePi_Plus.
  • Step 4. Connect the Raspberry to PC through USB cable.

Software

caution

If you are using Raspberry Pi with Raspberrypi OS >= Bullseye, you have to use this command line only with Python3.

  • Step 1. Follow Setting Software to configure the development environment.
  • Step 2. Git clone the Github repository.
cd ~
git clone https://github.com/DexterInd/GrovePi.git

  • Step 3. Excute below commands to monitor the switch status.
cd ~/GrovePi/Software/Python
python3 grove_switch.py

Here is the grove_switch.py code.

import time
import grovepi

# Connect the Grove Switch to digital port D3
# SIG,NC,VCC,GND
switch = 3

grovepi.pinMode(switch,"INPUT")

while True:
try:
print(grovepi.digitalRead(switch))
time.sleep(.5)

except IOError:
print ("Error")
  • Step 4. We will see the switch status as below.
pi@raspberrypi:~/GrovePi/Software/Python $ python3 grove_switch.py 
1
1
0
0
0

Schematic Online Viewer

Resources

Projects

Using a Switch to Open and Close a Relay: You will learn the value of a switch, with its High and Low function. In addition you will learn how to use a relay as an actuator.

Tech Support

Please submit any technical issue into our forum.

Loading Comments...