Go Down

Topic: Change functionality of mechanical switches (Read 1 time) previous topic - next topic

darkenney

Jan 01, 2019, 12:24 am Last Edit: Jan 01, 2019, 02:38 am by darkenney
First project, first fritz.

I have a toy that has two make-break mechanical switches (two pin). Press release the button of switch1 to increase speed a small amount. Press release the button of switch2 to decrease speed a small amount. To get the speed up to 30 for example, press release switch1 button 30 times. To bring speed back down to 0, press release switch2 button 30 times. Each switch makes 5v across its pins when its button is pressed. The two switches share a common ground. The toy internals know to speed up or slow down based on the button/switch that was pressed.

The goal is to use arduino to get those buttons to support press and hold functionality to continuously increase and decrease speed.

For testing, I took the two buttons off the toy and using Arduino I put together a simple two-button, blink-two-led setup. Press and hold button1 and LED1 will blink. Press and hold button2 and LED2 will blink. The thought being that I then replace the two LEDs with the speed up hot/ground and the slow down hot/ground wires of the toy. If Arduino can make the LED blink with a press and hold of the button, it will 'blink' the 5v to speed up and slow down the toy. The little arduino only setup works fine. All good on Arduino - press and hold each button and the respective LED blinks.

But it doesn't work when I replace the LEDs with the wires of the toy. When a single of the switches is connected to Arduino and toy, it works as desired -- pressing and holding increases speed or decreases speed as desired. When both of the switches and their wires from the toy are connected, neither works.

As mentioned, I get what I want with the Arduino using the switches, and LEDs. When I replace LEDs with wires of the toy, nothing.

Any thoughts about where to head next with this are appreciated.  



Code: [Select]
const int button1Pin = 2;    
const int led1Pin =  3;    
const int button2Pin = 7;    
const int led2Pin =  8;    
int button1State = 0;        
int button2State = 0;  

 void setup() {
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
}

void loop() {
  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);
  
  if (button1State == HIGH) {
    digitalWrite(led2Pin, LOW);
    digitalWrite(led1Pin, HIGH);
     delay(100);
    digitalWrite(led1Pin, LOW);  
      delay(100);
              
    
    } else if (button2State == HIGH) {
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, HIGH);
      delay(100);
    digitalWrite(led2Pin, LOW);  
      delay(100);
  
    } else {
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, LOW);
    }
}

Railroader

What is the current consumption of "the toy"? 20 mA is the maximum of several Arduino outputs.
How is "the toy" supplied with power?

Check the advice about how to attach info like code and wiring diagrams. We don't want to download, create folders and start Arduino IDE to see Your code.
Use Your knowledge. If that's not enough, look for education.
Having knowledge, think outside the box to gain more of it. Only trains run like the train, on the rails. The rest run between the rails.

MorganS

There's only two buttons or switches on the toy? If there were more (that you didn't tell us about) then I would suspect that the buttons are matrixed like a keyboard.

What is the voltage across each switch when it's not pressed? What is the voltage between the switches? Does that change when one is pressed?
"The problem is in the code you didn't post."

darkenney

#3
Jan 01, 2019, 02:36 am Last Edit: Jan 01, 2019, 02:53 am by darkenney
It's 2 x 6V 4.5AH rechargeable batteries. I would have to research more to find current consumption.

There are two other buttons. One is a rocker switch that functions as a master power on/off switch. Nothing works unless the master is on. The other switch is an on/off rocker for a small light.

Each switch shows ~0v when it's not pressed. Also 0v between switches - pressed or not. When a single switch is pressed, it will read ~4.5v; the other switch will continue to read 0v.

Thank you for the time and effort.

Paul__B

Each switch makes 5v across its pins when its button is pressed. The two switches share a common ground.
Is that completely correct?  The switches connect to ground - the negative rail?

But it doesn't work when I replace the LEDs with the wires of the toy. When a single of the switches is connected to Arduino and toy, it works as desired -- pressing and holding increases speed or decreases speed as desired. When both of the switches and their wires from the toy are connected, neither works.

OK, now if the buttons on your toy do switch to ground, that is an important design point - push-buttons - unless in a matrix - should switch to ground, not to the supply rail.  You show your buttons connected to the Arduino 5 V which means that you must unnecessarily wire the supply rail to more parts of the circuit than necessary and increase the chance of an accidental short circuit as well as interference pickup.  You should always make a practice of connecting the switches to ground and using a pinMode of INPUT_PULLUP; in many or most cases you will not need to provide a pull-up.

Now, you do not actually describe how you connect the Arduino to the buttons on the toy.  Needless to say, you must have the Arduino ground connected to the toy battery ground (negative).

What you need to do is to have it so the Arduino pulls the button inputs to ground to emulate the button.  To do this, you put a diode between the Arduino output (the diode cathode) and the toy button connection (the diode anode) and write the output LOW to implement the action (having previously set it HIGH before you initialise it as an output).  And to avoid the toy feeding current into the Arduino through the diode when the Arduino is OFF, you have to ensure the Arduino is switched on before the toy (or powered from it) and only off after the toy.

DrDiettrich

Button emulation with optocouplers is the easiest one. Respect the voltage polarity on the buttons, to match the opto transistor polarity, that's the only critical point.

wvmarle

+1 for optocouplers.
Replace the switches by optocouplers, and you're electrically completely isolated from whatever that toy uses.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

darkenney

Thank you for the detailed info. Still trying to get it implemented as described, also ordered a few optocouplers. Will keep at it. Thanks again.

Paul__B

Still trying to get it implemented as described, also ordered a few optocouplers.
Note - do not buy obsolete devices such as 4N25.

darkenney

Success. With the optocouplers, this became straightforward (even for me). Arduino powers the side with my buttons. When the optocoupler closes, it completes the circuit using just the toy's power wire and ground. Thanks all for the support.

darkenney

My setup works, but there is an aspect I'm not understanding. For my mechanical button with just two pins, I have one pin of the button connected to Arduino Gnd and the other pin of the button to  Arduino pin 2 - pinMode(2, INPUT_PULLUP);

When I do my digitalRead on pin 2, I get LOW when the button is pressed and the code acts on the LOW and all ok.

Here's my confusion - so what's the 'complete circuit' in this scenario?

Paul__B

Here's my confusion - so what's the 'complete circuit' in this scenario?
The circuit is from ground, through your button and through the internal pull-up device inside the Arduino, to Vcc.

darkenney

Ok.... I'm with you there sort of, but when the button is pressed and Input goes LOW  doesn't that break the circuit? And then without the button pressed, there can't be a completed circuit. So either way, no completed circuit.... maybe I'm being too literal here... thanks for the quick reply and guidance.

DrDiettrich

An input must be pulled up or down for a valid state. By default it's pulled high by the built-in pullup, which is overridden to low by the pressed switch. Even if the input current is very low, an input pin can sink or source current, just as required.

wvmarle

An Arduino input is usually considered an open circuit. It has a very high input impedance, which for most practical purposes can be considered infinite and the resulting current into the pin zero, just remember that it does take a tiny amount of current, in the order of a few pA. That's also why an unconnected pin is floating, and can read high or low randomly.

The internal pull-up resistor effectively connects it to Vcc when the switch is open, the switch connects it to GND when closed.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Go Up