X
Home > Blog > STEM for Arduino > HOW to Detect Magnetic Field with Kidspico

HOW to Detect Magnetic Field with Kidspico

By r November 21st, 2024 104 views
After the lightning strike, a certain amount of electricity will generated in the surrounding, and a strong magnetic field will be produced in an instant.
This magnetic field interferes with the connection of the relevant equipment of the station, thus disabling the automatic devices. Therefore, it is necessary to detect the magnetic field at all times and alarm immediately when the magnetic field is detected.
In this experiment, we utilize a Hall sensor and a white LED module to detect magnetic field. When the Hall sensor detects a magnetic field, the white LED lights up.

Flow

4301

Assembly

line1

Required Parts

43_00

line1

Step 1

43_01

line1

Step 2

43_02

line1

Step 3

43_03

line1

Step 4

43_04

line1

Step 5

43_05

line1

Step 6

43_06

line1

Step 7

43_07

line1

Completed

43_08

line1

Wiring Diagram

4302

Test Code

Open 4.3Magnetic field detection.py and click 1407.

'''
 * Filename    : Magnetic field detection
 * Thonny      : Thonny 4.1.4
 * Auther      : http//www.keyestudio.com
'''
from machine import Pin
import time

hall = Pin(8, Pin.IN)
led = Pin(11, Pin.OUT)
while True:
    value = hall.value()
    print(value, end=' ')
    if value == 0:
        led.on()
        print("A magnetic field")
    else:
        led.off()
        print("There is no magnetic field")
    time.sleep(0.1)

Code Explanation

5top

Conceive:

When the Hall sensor detects a magnetic field, a low power(0) will be output, and the white LED lights up. If there is no magnetic field, high(1) will be output, and the LED goes off.

line2

Code structure:

  1. Initialization. Set the pins of the Hall sensor and the white LED module.

  2. Loop.

    Print the digital value of the power level of the Hall sensor.

    Determine whether the value equals 0 (0 means a magnetic field is detected).

    • value = 0: the white LED lights up.

    • value ≠ 0: the white LED goes off.

5bottom

Test Result
4top
After uploading the code, the Hall sensor starts to detect magnetic field.
If the sensor detects a magnetic field, the white LED will turn on. If not, the LED will go off.
4303
4bottom
HOW to Simulate a Fire Alarm with Kidspico
Previous
HOW to Simulate a Fire Alarm with Kidspico
Read More
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Next
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Read More
Message Us