รหัส Arduino และวิดีโอสำหรับโมดูลเซนเซอร์ฮอลล์ KY-024
ในบทช่วยสอนนี้ เราจะสำรวจวิธีการใช้โมดูลเซนเซอร์ฮอลล์ KY-024 ร่วมกับ Arduino เพื่อตรวจจับสนามแม่เหล็ก เซนเซอร์ฮอลล์จะถูกใช้เพื่อกระตุ้นการทำงานเมื่อตรวจพบสนามแม่เหล็ก และเราจะแสดงผลลัพธ์บน Serial Monitor ผลลัพธ์ที่ได้คือวิธีที่ง่ายแต่มีประสิทธิภาพในการรวมการตรวจจับแม่เหล็กเข้ากับโปรเจกต์ของคุณ ซึ่งเป็นพื้นฐานสำหรับการขยายผลต่อไป สำหรับคำอธิบายโดยละเอียดเพิ่มเติม โปรดดูวิดีโอ (ในวิดีโอที่ 00:00)


อธิบายฮาร์ดแวร์
โมดูลเซนเซอร์ฮอลล์ถูกออกแบบมาเพื่อตรวจจับสนามแม่เหล็ก และมักใช้ในการใช้งานต่างๆ เช่น การตรวจจับความใกล้ชิดและการตรวจจับความเร็ว เมื่อมีสนามแม่เหล็กปรากฏ เซนเซอร์จะส่งสัญญาณที่ Arduino สามารถอ่านได้ ซึ่งช่วยให้ Arduino ทราบเมื่อมีวัตถุที่มีสนามแม่เหล็กอยู่ใกล้
ในโปรเจกต์นี้ เราจะใช้บัซเซอร์เป็นอุปกรณ์ที่ทำหน้าที่ เมื่อเซนเซอร์ฮอลล์ตรวจพบสนามแม่เหล็ก Arduino จะเปิดใช้งานบัซเซอร์เพื่อส่งเสียงเตือน ส่วนประกอบพื้นฐานที่เกี่ยวข้อง ได้แก่ เซนเซอร์ฮอลล์ บอร์ด Arduino และบัซเซอร์
รายละเอียดจาก Datasheet
| ผู้ผลิต | Honeywell |
|---|---|
| หมายเลขชิ้นส่วน | SS495A1 |
| แรงดันไฟฟ้าลอจิก/IO | 4.5 – 10 โวลต์ |
| แรงดันไฟฟ้าจ่าย | 4.5 – 10 โวลต์ |
| กระแสเอาต์พุต (ต่อช่อง) | สูงสุด 20 มิลลิแอมป์ |
| กระแสสูงสุด (ต่อช่อง) | สูงสุด 50 มิลลิแอมป์ |
| คำแนะนำความถี่ PWM | ไม่มี |
| เกณฑ์ลอจิกอินพุต | 0.7 โวลต์ (สูง), 0.3 โวลต์ (ต่ำ) |
| แรงดันตก / RDS(on) / ความอิ่มตัว | สูงสุด 0.4 โวลต์ |
| ขีดจำกัดความร้อน | -40 ถึง +85 °เซลเซียส |
| แพ็กเกจ | TO-92 |
| หมายเหตุ / รุ่นต่างๆ | มีให้เลือกหลายระดับความไว |
- ตรวจสอบให้แน่ใจว่าแหล่งจ่ายไฟอยู่ในช่วงแรงดันไฟฟ้าที่กำหนด
- ระวังขีดจำกัดกระแสเอาต์พุตเพื่อหลีกเลี่ยงความเสียหายต่อโมดูล
- ใช้ตัวต้านทานแบบดึงขึ้นหากจำเป็นเพื่อให้การอ่านค่ามีความเสถียร
- เก็บเซนเซอร์ให้ห่างจากสนามแม่เหล็กไฟฟ้าที่แรงซึ่งอาจทำให้เกิดการรบกวน
- ปรับเทียบเซนเซอร์ตามความต้องการเฉพาะของการใช้งาน
คำแนะนำการเดินสายไฟ


ในการเดินสายไฟโมดูลเซนเซอร์ฮอลล์เข้ากับ Arduino ให้เริ่มต้นโดยเชื่อมต่อพิน VCC ของเซนเซอร์เข้ากับพิน 5V ของ Arduino เพื่อจ่ายไฟ จากนั้นเชื่อมต่อพิน GND เข้ากับพิน GND ของ Arduino เพื่อให้วงจรสมบูรณ์ พินเอาต์พุตของเซนเซอร์ฮอลล์ ซึ่งโดยทั่วไประบุเป็น OUT ควรเชื่อมต่อกับพินดิจิทัลหมายเลข 2 บน Arduino
สำหรับบัซเซอร์ ให้เชื่อมต่อขั้วบวกเข้ากับพินดิจิทัลหมายเลข 8 บน Arduino และเชื่อมต่อขั้วลบเข้ากับ GND การตั้งค่านี้ช่วยให้ Arduino อ่านเอาต์พุตของเซนเซอร์และเปิดใช้งานบัซเซอร์เมื่อตรวจพบสนามแม่เหล็ก

ตัวอย่างโค้ดและคำอธิบาย
โค้ดต่อไปนี้เริ่มต้นเซนเซอร์ฮอลล์และบัซเซอร์ โดยกำหนดพินสำหรับเซนเซอร์และการทำงาน ลูปหลักจะอ่านสถานะของเซนเซอร์และเปิดใช้งานบัซเซอร์ตามนั้น
#define DETECT 2 // พิน 2 สำหรับเซนเซอร์
#define ACTION 8 // พิน 8 สำหรับการทำงานบางอย่าง
void setup() {
Serial.begin(9600);
pinMode(DETECT, INPUT); // กำหนดพินอินพุตสำหรับการตรวจจับ
pinMode(ACTION, OUTPUT); // กำหนดพินเอาต์พุตสำหรับ ACTION
}
ในโค้ดนี้ ตัวแปร DETECT ถูกกำหนดให้กับพิน 2 ซึ่งใช้สำหรับอ่านค่าเซนเซอร์ ตัวแปร ACTION ถูกกำหนดให้กับพิน 8 ซึ่งเชื่อมต่อกับบัซเซอร์ ฟังก์ชัน setup เริ่มต้นการสื่อสารแบบอนุกรมและกำหนดโหมดของพิน


void loop() {
int detected = digitalRead(DETECT); // อ่านค่าเซนเซอร์ฮอลล์
if (detected == LOW) {
digitalWrite(ACTION, HIGH); // เปิดบัซเซอร์
Serial.println("Detected!");
} else {
digitalWrite(ACTION, LOW); // ปิดบัซเซอร์
Serial.println("Nothing");
}
delay(200);
}
ส่วนนี้ของโค้ดจะตรวจสอบสถานะของเซนเซอร์ฮอลล์อย่างต่อเนื่อง หากเซนเซอร์ตรวจพบสนามแม่เหล็ก (เมื่อค่า detected เป็น LOW) จะเปิดบัซเซอร์และพิมพ์ "Detected!" ลงใน Serial Monitor หากไม่พบสนามแม่เหล็ก จะปิดบัซเซอร์และพิมพ์ "Nothing"
การสาธิต / สิ่งที่คาดหวัง
เมื่อคุณรันโค้ด Arduino จะตรวจสอบเซนเซอร์ฮอลล์อย่างต่อเนื่องเพื่อหาสนามแม่เหล็ก หากตรวจพบสนามแม่เหล็ก บัซเซอร์จะส่งเสียง และคุณจะเห็น "Detected!" พิมพ์บน Serial Monitor หากไม่มีสนามแม่เหล็ก บัซเซอร์จะปิดอยู่ และจะแสดง "Nothing" ระมัดระวังในการเดินสายไฟเพื่อหลีกเลี่ยงการเชื่อมต่อกลับขั้ว ซึ่งอาจทำให้การตรวจจับล้มเหลว (ในวิดีโอที่ 06:15)
เวลาต่างๆ ในวิดีโอ
- 00:00 - แนะนำโมดูลเซนเซอร์ฮอลล์
- 02:30 - คำอธิบายการเดินสายไฟ
- 05:00 - คำอธิบายโค้ด
- 08:15 - การสาธิต
/*
* This is the Arduino code for a Hall Sensor module for Arduino.
// Written for Robojax.com video
* A Hall sensor switch will detect the magnetic field, and the module is used to trigger something.
* Watch the video for details: https://youtu.be/QH1Lw9BwTJI
* Code is available at: http://robojax.com/learn/arduino
*
Written by Ahmad Nejrabi for Robojax.com
on January 28, 2018 at 19:41 in Ajax, Ontario, Canada
* Permission granted to share this code, given that this
* note is kept with the code.
* Disclaimer: This code is "AS IS" and for educational purposes only.
*
*/
/*
*/
// Hall Sensor code for Robojax.com
#define DETECT 2 // pin 2 for sensor
#define ACTION 8 // pin 8 for action to do something
void setup() {
Serial.begin(9600);
Serial.println("Robojax.com Hall Module Test");
pinMode(DETECT, INPUT);//define detect input pin
pinMode(ACTION, OUTPUT);//define ACTION output pin
// Flame sensor code for Robojax.com
}
void loop() {
// Hall Sensor code for Robojax.com
int detected = digitalRead(DETECT);// read Hall sensor
int detectedAn = analogRead(A0);// read flame analog value
if( detected == LOW)
{
digitalWrite(ACTION,HIGH);// set the buzzer ON
Serial.println("Detected!");
// Serial.println(detectedAn);// print analog value
}else{
digitalWrite(ACTION,LOW); // Set the buzzer OFF
Serial.println("Nothing");
// Hall Sensor code for Robojax.com
}
delay(200);
}
สิ่งที่คุณอาจต้องการ
-
อเมซอน
-
อเมซอน
-
อเมซอน
-
อาลีเอ็กซ์เพรสBuy KY-024 Linear Magnetic Hall Switch from AliExpresss.click.aliexpress.com
-
อาลีเอ็กซ์เพรสBuy KY-024 Linear Magnetic Hall Switch from AliExpress (2)s.click.aliexpress.com
แหล่งข้อมูลและเอกสารอ้างอิง
-
ภายนอกHall Sensor Datasheet (PDF)elecrow.com
ไฟล์📁
แผ่นข้อมูล (pdf)
-
SS49E_SS39ET_SS59ET_hall_sensor_honeywell_49EThe SS39ET/SS49E/SS59ET Series low-cost linear Hall-effect sensor ICs are small, versatile devices that are operated by the magnetic field from a permanent magnet or an electromagnet. They are designed and manufactured for cost competitiveness. The linear sourcing output voltage is set by the supply voltage and varies in proportion to the strength of the magnetic field. Low voltage capability as low as 2.7 Vdc and reduced current consumption of only 6 mA typically at 5 Vdc help make this product energy efficient. The integrated circuitry features low noise output, which makes it unnecessary to use external filtering. These sensor ICs Interface with many electrical components without buffering. They also include thin film resistors to provide increased temperature stability and accuracy. These linear Hall-effect sensor ICs have an operating temperature range of -40 °C to 100°C [-40 °F to 212 °F], appropriate for industrial and medical environments. Thermal balancing allows for stable operation over the full temperature range. They are available in three package styles, all of which may be supplied on tape for automated, lower-cost assembly: • SOT-23: SS39ET. This small footprint takes up less space on the PC board, typically allowing for more components. • Flat TO-92-style, with different lead configurations: SS49E, SS49E-L, SS49E-F. • SOT-89B: SS59ET.
SS49E_SS39ET_hall_sensor_honeywell_49E.pdf0.98 MB -
49E YANGZHOU Hall-effect linear hall sensorThe 49E Series Economical Linear Hall-effect sensors are small, versatile linear Hall-effect devices that are operated by the magnetic field from a permanent magnet or an electromagnet. The linear sourcing output voltage is set by the supply voltage and varies in proportion to the strength of the magnetic field. The integrated circuitry features low noise output, which makes it unnecessary to use external filtering. It also includes thin film resistors to provide increased temperature stability and accuracy. These linear Hall sensors have an operating temperature range of -40 °C to 100 °C, appropriate for commercial, consumer, and industrial environments.
49E_YANGZHOU_Hall-effect_linear_hall_sensor.pdf0.49 MB -
SS49E SS39ET Hall Sensor Honeywell 49EDatasheet for Honeywell SS49E and SS39ET linear Hall-effect sensor ICs. These sensors provide analog output proportional to magnetic flux density, operate from 4.5V to 10.5V, and are available in a small SIP package.
SS49E_SS39ET_hall_sensor_honeywell_49E.pdf0.98 MB