Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize accelerometer with MicroPython

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to realize the accelerometer with MicroPython". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to realize the accelerometer with MicroPython" can help you solve the problem.

1. The use of accelerometer

There is an accelerometer on the development board that can detect angle and motion. ). There are different sensors on the X, Y and Z axes. You can get the value of the accelerometer by creating a pyb.Accel () object and calling the x () method. Radish education tpyboard. Com

> > accel = pyb.Accel ()

> > accel.x ()

The above example returns a signed angle value between-30 and 30. Note that the measurements are not accurate, which means that even if the tpyboard is completely stationary, there will still be measurements. Therefore, the data obtained by the x () method can not be used as an exact value, but as a range of values with a certain precision.

The code for tilting the development board and lighting the LED light through the accelerometer is as follows:

Accel = pyb.Accel () light = pyb.LED (3) SENSITIVITY = 3 while True: X = accel.x () if abs (x) > SENSITIVITY: light.on () else: light.off ()

In the above code, we create two objects, Accel and LED, and then directly get the value of the accelerometer in the X direction. If the size of the x value is larger than the fixed value SENSITIVITY, the LED lamp will be lit, otherwise it will be turned off. The pyb.delay () function is called in the loop, otherwise the LED light will flash very frequently when the value of x is close to SENSITIVITY. Try running the program on the tpyboard development board until you tilt the development board left and right to turn the LED light on or off.

Exercise: change the above script so that the larger the tilt angle, the brighter the blue LED light.

Tip: you need to readjust the value, which is between 0 and 255

2. Make the level meter

The above routine only uses the angle value in the x direction, but we can use the value of the y () function and more LED lights to turn the tpyboard development board into a level.

Xlights = (pyb.LED (2), pyb.LED (3)) ylights = (pyb.LED (1), pyb.LED (4)) accel = pyb.Accel () SENSITIVITY = 3 while True: X = accel.x () if x > SENSITIVITY: xlights [0] .on () xlights [1] .off () elif x

< -SENSITIVITY: xlights[1].on() xlights[0].off() else: xlights[0].off() xlights[1].off() y = accel.y() if y >

SENSITIVITY: ylights [0] .on () ylights [1] .off () elif y <-SENSITIVITY: ylights [1] .on () ylights [0] .off () else: ylights [0] .off () ylights [1] .off () pyb.delay

We started by creating a tuple containing LED objects in the x and y directions. Tuples in the python language are immutable objects, which means that once created, they cannot be changed. Then we start as we did in the previous routine, but turn on different LED lights when the value of x is positive or negative. It's the same principle in the y direction. This doesn't look very good, B, but it does realize the function of the level meter. Run this program on your tpyboard board, and the phenomenon is to tilt the development board in different directions to light up different LED lights.

This is the end of the introduction about "how to realize the accelerometer in MicroPython". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report