Lesson 3 Switch Hall Sensor

Share for us

Introduction

A switch Hall sensor consists of voltage regulator, Hall element, differential amplifier, Schmitt trigger, and output terminal. It outputs digital values.

Components

– 1 * SunFounder Uno board

– 1 * USB data cable

– 1 * Switch Hall sensor module

– 2 * 3-Pin anti-reverse cable

– 1 * Passive buzzer

– 1 * Magnet

Experimental Principles

Hook up the switch Hall sensor module with pin 8 of the SunFounder Uno, and the buzzer to pin 7. When an energized conductor approaches the module, the output terminal SIG outputs low level; the buzzer beeps and at the same time the corresponding LED lights up.

Experimental Procedures

Step 1: Build the circuit

The wiring between the switch Hall sensor and SunFounder Uno board:

Switch Hall SensorSunFounder Uno
SIG8
VCC5V
GNDGND

The wiring between the passive buzzer and SunFounder Uno board:

Passive BuzzerSunFounder Uno
SIG7
VCC5V
GNDGND

Step 2: Program (Please refer to the example code in LEARN -> Get Tutorials on our website)

Step 3: Compile

Step 4: Upload the sketch to SunFounder Uno board

Now, if a magnet approaches the switch Hall sensor, the indicator light on the sensor will light up and the buzzer will beep. At the same time, the LED attached to pin 13 on the SunFounder Uno board will also light up.

Before

After

Code

// Hall Switch Sensor
const int hallPin = 8; //the SIG of hall sensor attach to
const int ledPin = 13; //pin 13 built-in LED light
const int buzzerPin=7;// Passive Buzzer SIG
int val = 0; //the variable to store the value read from Hall Switch
/********************************/
void setup()
{
  pinMode(hallPin,INPUT); //initialize the hall as an input
  pinMode(ledPin,OUTPUT); //initialize the led as an output
  pinMode(buzzerPin,OUTPUT);// initialize The Passive Buzzer as an output
  Serial.begin(9600);// initialize serial communications at 9600 bps}
/**********************************/
void loop()
{
  val = digitalRead(hallPin); //read the value from hall Switch
  Serial.println(val);//print the value
  if(val == HIGH)
  {
    digitalWrite(ledPin,LOW);//turn off led
    noTone(7);//if you want to play different pitches on multiple pins, you need to     call noTone() on one pin before calling tone() on the next pin.
  }
  if(val == LOW)
  {
    digitalWrite(ledPin,HIGH);//turn on led
   tone(7,320,200);//generate a tone on pin 7 ,with a frequency of 320hz and the    duration of the tone is 200 milliseconds
  }
  delay(500);//to get a beeping tone
}
/*************************************/