Advertisement
belrey10

[KY-003] Hall Magnetic Sensor

Nov 9th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 1.83 KB | Source Code | 0 0
  1. /*
  2.  * inventr.io 37 in 1 Sensor Kit (https://learn.inventr.io/product/37-in-1-sensor-kit/)
  3.  * Sensor Course (https://learn.inventr.io/course/sensor-training/)
  4.  *
  5.  * Lesson - [KY-003] Hall Magnetic Sensor
  6.  *
  7.  * The KY-003 is a magnetic Hall-effect sensor. It can detect the presence of a magnetic
  8.  * field and output an electrical signal proportional to the strength of the magnetic
  9.  * field. The sensor operates by using the Hall effect, which is the production of a
  10.  * voltage difference across an electrical conductor, transverse to an electric current
  11.  * in the conductor and a magnetic field perpendicular to it. The KY-003 is typically
  12.  * used in various applications such as detecting the position of a rotating magnet,
  13.  * or as a switch triggered by a magnet.
  14.  *
  15.  * KY-003 sensor pin is HIGH until a magenetic field is detected, when sensor pin goes LOW.
  16.  *
  17.  * Code contributions by David Schmidt (davids@inventr.io)
  18.  */
  19.  
  20. /* This project just needs a Digital pin.
  21.  * On the Hero (Arduino Uno compatible) we *could* use: D0-D13, A0-A5.
  22.  * Skip: A0-A5 (save for Analog),
  23.  *       D0/D1 (used by USB),
  24.  *       D2/D3 (save for interrupts),
  25.  *       D13 (used by LED_BUILTIN and SPI Clock),
  26.  *       D5, D6, D9, D10 and D11 (save for PWM)
  27.  *       D11 (SPI MOSI)
  28.  *       D12 (SPI MISO)
  29.  * Recommended for fewest conflicts:
  30.  *    D4, D7 or D8
  31.  */
  32. const uint8_t KY_003_PIN = 10;  // define the Hall magnetic sensor interface
  33.  
  34. void setup() {
  35.   pinMode(LED_BUILTIN, OUTPUT);       // define LED as output interface
  36.   pinMode(KY_003_PIN, INPUT_PULLUP);  // define the Hall magnetic sensor line as input
  37. }
  38.  
  39. void loop() {
  40.   if (digitalRead(KY_003_PIN) == LOW) {  // when the Hall sensor detects a magnetic field (LOW), Arduino LED lights up
  41.     digitalWrite(LED_BUILTIN, HIGH);
  42.   } else
  43.     digitalWrite(LED_BUILTIN, LOW);
  44.   delay(100);
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement