Required Components


About Reed Switches

There are two metal plates inside, and when a magnet is brought close, they overlap and conduct electricity. The term "reed" in a reed switch comes from the fact that the shape of the metal plate is similar to the reed used in woodwind instruments (such as clarinets).

Pull-up and Pull-down Resistors

When using a switch, you might have used a circuit like the one below when you were a student.

This circuit doesn't actually not work.
When the switch is pressed, 3.3V is applied to the RBoard. So, what signal is applied to the RBoard when the switch is not pressed? The answer is uncertain. Since it's not connected to anything, we can't know what signal will be applied without actually testing it. To avoid this situation, pull-up and pull-down resistors are used.

When using pull-up and pull-down resistors, even when the button is not pressed, some signal will be applied to the RBoard. γ€€β‡’ In the case of pull-up, 3.3V; in the case of pull-down, 0V (GND) You might think that even without the resistor, it would be connected, but if you don't put a resistor, pressing the switch will cause a short circuit between 3.3V and GND. Also, among pull-up and pull-down, pull-up is used more frequently. One reason is that with pull-down, it's difficult to notice when a wire breaks. If a wire breaks as shown below, it will be in the same state as the circuit described earlier.


By using the pull-up and pull-down resistors built into the microcontroller, the circuit can be simplified.


Electronic Circuit

Breadboard circuit using built-in pull-up resistors
Please connect using the connector cable.


Program

A program that turns on an LED when the door is open.

REED_SW = 18
pinMode(REED_SW, 1)
pinPull(REED_SW, 1)
while  true
  status = digitalRead(REED_SW)
  # Because it's pulled up, the status will be 0 when the door is closed.
  if(status == 1)
    digitalWrite(0, 1)
  else
    digitalWrite(0, 0)
  end
end
When using pull-up and pull-down resistors, use pinPull(pin number, setting value). The setting value is 0 for the normal state, 1 or other positive integers for pull-up, and -1 or other negative integers for pull-down.
To get the pin status, use digitalRead(pin number).

Task: