801 views|3 replies

35

Posts

4

Resources
The OP
 

[Digi-Jet Follow me Issue 3] XIAO motherboard reads magnetic switch [Copy link]

 

Hello everyone, I am Zheng Gong. I bought a magnetic switch in the third issue of Follow me. This post will introduce how to read the magnetic switch with XIAO motherboard.

As the name suggests, a magnetic switch is a switch that can be opened and closed by a magnet. It can be used as a hidden switch in situations where users do not want to touch it.

The above is the magnetic switch of the GROVE interface. You can see that the connection is power/ground and there is also a signal (SIG) interface. It can be directly connected to the ADC interface on the XIAO baseboard.

The usage is also very simple, just need to initialize the interface corresponding to the AD port as a digital input port. The interface definition is as follows

The corresponding pin ports are:

So the initialization code is as follows

from machine import Pin

p2 = Pin(2, Pin.IN)

Then use p2.value() to read the value of the specific port.

My entire code is as follows. Since it is relatively simple, I will not upload it as an attachment.

from machine import Pin, SoftI2C
import ssd1306
import math
import time


p2 = Pin(2, Pin.IN)
i2c = SoftI2C(scl=Pin(7), sda=Pin(6))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)

oled.fill(0)
oled.text("pin2 off", 10, 15)
oled.fill_rect(40, 40, 20, 20, 1)
oled.show()  # Show the text
p2_status = False
while True:
    if p2.value() == True:
        if p2_status == False:
            p2_status = True
            oled.fill(0)
            oled.text("pin2 on", 10, 15)
            oled.fill_rect(40, 40, 20, 20, 1)
            oled.show()  # Show the text
    else:
        if p2_status == True:
            p2_status = False
            oled.fill(0)
            oled.text("pin2 off", 10, 15)
            oled.fill_rect(40, 40, 20, 20, 1)
            oled.show()  # Show the text

This post is from DigiKey Technology Zone

Latest reply

What is the approximate detection distance of this sensor?   Details Published on 2024-1-29 14:32
 
 

6580

Posts

0

Resources
2
 

Mr. Zheng explained clearly how to read the magnetic switch on the XIAO motherboard

This post is from DigiKey Technology Zone
 
 
 

6748

Posts

2

Resources
3
 

What is the approximate detection distance of this sensor?

This post is from DigiKey Technology Zone

Comments

It's only one or two centimeters, which may also be related to the magnetism of the magnet  Details Published on 2024-7-10 21:46
 
 
 

35

Posts

4

Resources
4
 
wangerxian posted on 2024-1-29 14:32 What is the approximate detection distance of this sensor?

It's only one or two centimeters, which may also be related to the magnetism of the magnet

This post is from DigiKey Technology Zone
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Featured Posts
DIY RF Power Amplifier - Record the whole process

This post was last edited by RF-刘海石 on 2018-9-10 11:05 I haven't written a post here for a long time. I've been eithe ...

Wireless street light project——SX1278 debugging

Problem description:   msp430 can communicate with msp430, STM32 can communicate with STM32, but they cannot communic ...

Domestic 32-bit MCU compatibility test non-authoritative report - AT32F403 compatibility test

The trade war has intensified recently, and coupled with the sudden outbreak of the epidemic in 2020, the supply of 32 ...

Regarding power supply, let me share a particularly inspiring story...

I would like to share with you a little story I heard from teacher @maychang: Vicor CEO Patrizio Vinciarelli ...

Smart Camera Structural Design--Physical Assembly

After fine-tuning various details, we made a sample of a light-curing 3D and a CNC aluminum alloy sample using a machini ...

32 "Ten Thousand Miles" Raspberry Pi Car - Ubuntu system configured into Raspberry Pi system environment

If you are used to the Raspberry Pi operating system, you may feel uncomfortable switching to Ubuntu MATE. Next, confi ...

[Synopsys IP Resources] 97% of tested applications have security vulnerabilities. Is your software secure?

Synopsys recently released the "2021 Software Vulnerability Snapshot: An Analysis by Synopsys Application Security Testi ...

[HPM-DIY] HPM6750 peripheral LCDC driver RGB screen high frame rate video playback

The LCD peripheral functions of HPM6750 are relatively complete, and the underlying interfaces of the SDK are mostly com ...

[Flower carving hands-on] Interesting and fun music visualization series of small projects (19) - full-body fiber optic lamp

I suddenly had the urge to do a series of topics on music visualization. This topic is a bit difficult and covers a wide ...

Chapter 6 of "Detailed Explanation and Practice of Atomic Embedded Linux Driver Development" Pinctrl and GPIO Subsystem Learning and Combination Examples

The Linux kernel provides pinctrl and gpio subsystems for GPIO drivers Pinctrl Subsystem Linux drivers emphasize driver ...

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list