Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Reed Switches & Arduino

Reed Switches & Arduino

Demo of a reed switch connected to an Arduino to adjust the brightness of an LED.

GitHub repo: https://github.com/ucu-project/lab-reedswitch

Avatar for Matías Olivera

Matías Olivera

November 20, 2014
Tweet

More Decks by Matías Olivera

Other Decks in Programming

Transcript

  1. What is a Reed Switch? • Electrical switch operated by

    a magnetic field • Do not require someone or something to physically operate them • Magnetic energy needs to pass near enough to the switch to cause actuation
  2. How is a Reed Switch constructed? • Two or three

    leads that have thin reed-like contacts attached • The contacts and a portion of the leads are encased in a tubular glass capsule.
  3. Form A: Single Pull-Single Throw (NO) • Most common configuration

    • Two contacts, normally open • Can be wired without regard for the direction of electrical flow
  4. Form B: Single Pull-Single Throw (NC) • Least common configuration

    • Also two contacts, but normally closed • Can also be wired without regard for the direction of electrical flow
  5. Form C: Single Pull-Double Throw (NO/NC) • Quite common configuration

    • Three contacts: common, normally open and normally closed • Direction of electrical flow should be considered when wiring a Form C switch
  6. Lab

  7. Hardware • Arduino Uno board • Two reed switches separated

    by 10 cm are used to calculate the speed of a vehicle • Two LEDs connected in parallel
  8. Operation • First reed switch detects the presence of the

    vehicle, then the LED brightness is set to 100% • Second switch detects the presence of the vehicle, and its speed is calculated • LED brightness is brought back to 30% just at the time the vehicle passes beside it
  9. Code const int ledPin = 9; // the pin that

    the LED is attached to const int switchAPin = 2; // the pin that the reed switch A is attached to const int switchBPin = 3; // the pin that the reed switch B is attached to const byte partialBrightness = 77; // 30% of brightness based in a 0 to 255 scale const byte fullBrightness = 255; // 100% of brightness based in a 0 to 255 scale const float distance = 0.10; // distance between reed switches (10 cm) int switchAReading; int switchBReading; unsigned long switchATime; unsigned long switchBTime; void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); analogWrite(ledPin, partialBrightness); // set LED brightness to 30% // initialize the reed switches pins as inputs: pinMode(switchAPin, INPUT); pinMode(switchBPin, INPUT); }
  10. void loop() { // get a switch A reading switchAReading

    = digitalRead(switchAPin); if (switchAReading == HIGH) { analogWrite(ledPin, fullBrightness); // set LED brightness to 100% switchATime = millis(); // wait for trigger of switch B to start calculation - otherwise timeout do { switchBReading = digitalRead(switchBPin); if (millis() - switchATime > 5000) { analogWrite(ledPin, partialBrightness); // set LED brightness to 30% return; } } while (switchBReading == LOW); switchBTime = millis(); float timing = ((float) switchBTime - (float) switchATime)/1000; // computes time in seconds float speed = distance/timing; // computes speed in m/s float timeToLed = distance/speed*1000; // time to reach the LED in milliseconds delay(timeToLed); analogWrite(ledPin, partialBrightness); // set LED brightness to 30% } }
  11. Pull-up & pull-down resistors • Used to ensure that inputs

    to the Arduino settle at expected logic levels • A pull-up resistor pulls the voltage of the wire it is connected to towards its voltage source level when the other components on the line are inactive • A pull-down resistor works in the same way but is connected to ground
  12. “Just because you have nothing at all connected to an

    input pin doesn’t mean it is a logical zero.”