Help with Reed Sensor and code

Hey Guys,

I am a newbie to the Arduino and have started a couple of projects.

I really need some help with the latest one.

I am trying to build a speedometer for my motorcycle.

I have an Arduino Nano and have purchased this reed switch

https://www.digikey.com.au/product-detail/en/59060-1-T-05-A/HE622-ND/1829865

I am trying to use the following code.

With the above reed switch do I have pull up the pin that it is attached to? or have an external pull down resistor.

For the Nano, I had my reed switch connected to the D2 pin and the ground, is this correct?

Would that work with this line alone: attachInterrupt(0, speedCalc, RISING);

does the 0 assign to the D2 pin?

Any help would be greatly appreciated.

does the 0 assign to the D2 pin?

Yes, interrupt 0 is available on pin D2.

With the above reed switch do I have pull up the pin that it is attached to?

That's one way to do it.

For the Nano, I had my reed switch connected to the D2 pin and the ground, is this correct?

With the mentioned setup: yes.

GB250:
With the above reed switch do I have pull up the pin that it is attached to? or have an external pull down resistor.

For the Nano, I had my reed switch connected to the D2 pin and the ground, is this correct?

The reed switch has only two contacts, so you must either:

  • connect the switch between D2 and ground and use an external pull-up resistor,
  • connect the switch between D2 and 5V and use an external pull-down resistor,
  • connect the switch between D2 and ground use the Arduino's internal pull-up resistor with "pinMode(2, INPUT_PULLUP);"

Without one of the above, the Arduino pin will "float" and will not reliably read the switch.

However, option 2 is not recommended because of the risk of a short-circuit if the wire between the Arduino and the switch were to become disconnected or damaged. The bare 5V wire could touch the frame of the motorcycle, and because there would be no resistor to limit the current, the Arduino's 5V regulator could be damaged.

GB250:
Would that work with this line alone: attachInterrupt(0, speedCalc, RISING);

Using interrupts for this project is not necessary and not recommended for beginners. Use "digitalRead(2);". When the result changes from LOW to HIGH or HIGH to LOW, add one to a counter.