Sunday, August 17, 2014

Switches and Arduino

The last Arduino post I started with the analog inputs (the potentiometer). Analog should be more complicated than digital, but they aren't on the Arduino. Digital inputs are usually either on or off, and that is all. Adding wire makes it more complicated.

Most switches have two states, on or off. This is digital, high or low, if you are turning on a light. There are two wires coming out of the basic switch, and a digital port is only one wire. How would one connect a switch to a single bit on a computer board? The answer is using the GND pin(s). Connect one wire to the GND pin, and the other to the digital input that is needed to be controlled.

We can write a simple sketch to monitor digital pin 2 and turn the LED on the board on and off.

/*
  Button
  Turns on an LED on when button is pressed
  connected to pin 2. 
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
// Pin 2 has a button connected externally. 
int button = 2;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(button, INPUT_PULLUP);  
}

// the loop routine runs over and over again forever:
void loop() {
  int onOff;
  onOff = digitalRead(button);   // read the button state
  digitalWrite(led, onOff);      // set the LED to the state of the button
}

This sketch again starts very similar to the original Blink sketch, but there is a new global variable, "button". The button is the input pin to monitor for switch state. We need to tell the Arduino that this pin is an input, in the setup() function.

There is a new constant that is used in the pinMode() function, "INPUT_PULLUP". For input pins there are two modes, simple "INPUT" and "INPUT_PULLUP". INPUT mode is just that, the input will be monitored, for whatever comes in. The pin will generally indicate "HIGH" when nothing is connected to it, but sometimes long wires can make the pin go to a low state, depending on what is around those wires.Internally the CPU chip will have a small resistor than can force the pin to always be "HIGH" unless it is connected to ground (GND). Since the one pin on the switch is connected to GND, when the button is pressed, the pin will report "LOW". Using the "PULLUP" option will make the state of the pin on the computer report the state of the button more accurately.

The loop() function is very simple. It reads the state of the button, and sets the LED to that state. The digitalRead() function only needs the pin to read as an argument, and it returns the result that can be assigned to a variable "onOff".

If this sketch is run on the Arduino, the LED will turn off when the button is pressed. This may seem reversed, most of the time, when the button is pressed the LED should light up. The state seems reversed because the button is connected to ground. When the button is pressed, it forces the voltage to 0 (GND) and the digitalRead() function will set the onOff variable to "LOW". Setting the digitalWrite for the LED pin to be "LOW". When the button is released, the pullup on the button pin will make the voltage 3V and the digitalRead() on the pin will return "HIGH" in the onOff variable.

Logic can be changed, and the LED can turn on when the button is pressed. Using an "if()/else" statement can allow the results to be reversed similar to:

   if (onOff == HIGH)
      digitalWrite(led, LOW);
  else
      digitalWrite(led, HIGH);

Remember to use the "==" when comparing values in an if() statement. This is saying if the button is returning HIGH, set the LED to LOW, otherwise set the LED to HIGH.

This logic can be expanded to including multiple sensors, causing other alerts to occur.

Using other switch types can create more flexible monitoring. A push button requires great conformance of the switch to what is being monitored. A push button requires the item being monitored must hit the switch at the right time.

Other switch types, can allow flexibility of the orientation of the switch to the item being monitored. Lever type microswitches are the most flexible. A microswitch will only operate in a narrow region of the motion. The lever can absorb a great deal of motion.Toggle switches are good in the panel of the aircraft. Toggle switches have great tactile feel, and come in a variety of designs.

It is good to wire sensor wires to GND as much as possible. Running any source voltage (IE 12V, 5V etc) will require the wire to be fused to prevent the source and the wire from being damaged. There is a fuse or circuit breaker from the panel to all devices requiring source voltage in the aircraft. This is very important to prevent fires, low voltage and other problems in the aircraft.

What do you need next?


No comments:

Post a Comment