Slide 1

Slide 1 text

Arduino & Reed Switches

Slide 2

Slide 2 text

Matías Olivera @moliveraf

Slide 3

Slide 3 text

Agenda • Introducing Reed Switches • Lab • Show time • Appendix

Slide 4

Slide 4 text

Introducing Reed Switches

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

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.

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Lab

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Circuit

Slide 16

Slide 16 text

Schematic

Slide 17

Slide 17 text

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); }

Slide 18

Slide 18 text

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% } }

Slide 19

Slide 19 text

Show time…

Slide 20

Slide 20 text

Appendix

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

Pull-up & pull-down resistors Pull-up resistor Pull-down resistor Vin Vin Switch Switch Gnd Gnd Vout Vout

Slide 23

Slide 23 text

“Just because you have nothing at all connected to an input pin doesn’t mean it is a logical zero.”

Slide 24

Slide 24 text

Thanks.