Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- The MTS-302 is a simple mechanical switch and doesn't directly interface with code. However, you can read its state using a microcontroller like an Arduino. The MTS-302 is a 3P3T switch, meaning it has 3 poles and 3 throws. This can be treated as three individual SP3T (single pole, triple throw) switches.
- In this sketch, each throw of the switch is connected to a different digital input pin on the Arduino. The internal pull-up resistors are used, so you'll connect the other side of the switch to ground.
- Every half second, the sketch reads the state of the switch and prints it to the serial monitor. If all the switches are open (in the case of the MTS-302, this would mean the switch is in a position that isn't connected to one of the throws you're reading), it will print "Unknown".
- Remember to be careful with the switch ratings. The MTS-302 is rated for 250V and 3A, but your Arduino can't handle those levels. If you're switching something that uses high voltage or current, you'll need to use a relay or similar device.
- Here's a simple Arduino sketch that reads the state of one pole. You'd repeat this for each pole, using different input pins.
- */
- const int switchPin1 = 2; // connect this to one throw of the switch
- const int switchPin2 = 3; // connect this to the second throw of the switch
- const int switchPin3 = 4; // connect this to the third throw of the switch
- void setup() {
- pinMode(switchPin1, INPUT_PULLUP);
- pinMode(switchPin2, INPUT_PULLUP);
- pinMode(switchPin3, INPUT_PULLUP);
- Serial.begin(9600);
- }
- void loop() {
- int switchState1 = digitalRead(switchPin1);
- int switchState2 = digitalRead(switchPin2);
- int switchState3 = digitalRead(switchPin3);
- Serial.print("Switch position: ");
- if (switchState1 == LOW) {
- Serial.println("Position 1");
- } else if (switchState2 == LOW) {
- Serial.println("Position 2");
- } else if (switchState3 == LOW) {
- Serial.println("Position 3");
- } else {
- Serial.println("Unknown");
- }
- delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement