KY-003 และ KY-035 Hall effect magnetic sensor เป็น magnetic sensor การใช้งานง่ายมากครับ Outputเป็น Analog ต่อใช้งานดังนี้
ตัวอย่างSource Code
/*
Author: Danny van den Brande, Arduinosensors.nl. BlueCore Tech.
In this example we use the hall magnetic sensor as a magnetic
field detector. A simple but useful code.
*/
int Buzzer = 4;
int sensorPin = A0;
int GreenLed = 2;
int BlueLed = 3;
int sensorValue = 0;void setup () {
pinMode (BlueLed, OUTPUT);
pinMode (GreenLed, OUTPUT);
pinMode (Buzzer, OUTPUT);
Serial.begin (9600);
}void loop () {
sensorValue = analogRead (sensorPin);
if(analogRead(1)>575 && analogRead(1)< 700)
//You can play with these values, open serial
//monitor to see value readings.
{
digitalWrite (BlueLed, HIGH);
digitalWrite (Buzzer, HIGH);
delay(1000);
digitalWrite (BlueLed, LOW);
digitalWrite (Buzzer, LOW);
delay(100);
Serial.println(sensorValue, DEC);
}
else
{
digitalWrite (BlueLed, LOW);
digitalWrite (GreenLed, HIGH);
delay(1000);
digitalWrite (GreenLed, LOW);
digitalWrite (Buzzer, LOW);
delay(100);
Serial.println(sensorValue, DEC);
}}