【Micropython】ESP8266驱动mpu6050读取数据

【Micropython】ESP8266驱动mpu6050读取数据


  • 📌相关篇《【MicroPython ESP32】ssd1306驱动0.96“I2C屏幕+mpu6050图形控制

  • ✨本案例基于Thonny平台开发。✨

  • 🔖esp8266固件版本:MicroPython v1.19.1 on 2022-06-18

  • 📍本篇需要使用的加载的mpu6050模块库(无法在Thonny里面下载到):https://github.com/narutogo/micropython-mpu6050-

  • 🎈与之相关的库(兼容ESP32):https://github.com/Lezgend/MPU6050-MicroPython

🛠将库下载下来后,将模块导入进去

在这里插入图片描述

📝示例程序一

  • ✨主程序通过库所提供的测试代码上添加了温度数据打印。
from machine import I2C, Pin
import q4
import time,math
import mpu
import mpu6050
i2c = I2C(scl=Pin(5), sda=Pin(4))
acc = mpu6050.accel(i2c)
acc.error_gy()
#ay=acc.get_values()
#mpu.one_filter(ay["AcX"],ay["AcY"],ay["AcZ"],ay["GyX"],ay["GyY"],ay["GyZ"])
i=0
gyx=0
gyy=0
r=[]
q=[]
while 1:
	ay=acc.get_values()
	i=i+1
	if i==124:
		#print(r)
		print(q)
		i=0
		
		#print(ay["AcX"],ay["AcY"],ay["AcZ"],ay["GyX"],ay["GyY"],ay["GyZ"])
	#r=mpu.one_filter(ay["AcX"],ay["AcY"],ay["AcZ"],ay["GyX"],ay["GyY"],ay["GyZ"])
	q=q4.IMUupdate(ay["GyX"]/65.5*0.0174533,ay["GyY"]/65.5*0.0174533,ay["GyZ"]/65.5*0.0174533,ay["AcX"]/8192,ay["AcY"]/8192,ay["AcZ"]/8192)
	Temp = ay["Tmp"]
	print("Temp:",Temp,"°C",q)
	time.sleep(1)
	


  • 📋输出打印信息:
    在这里插入图片描述

📖示例程序二

  • 🔖此库可以将数据保存到.txt
    在这里插入图片描述
    在这里插入图片描述
# Example code for (GY-521) MPU6050 Accelerometer/Gyro Module
# Write in MicroPython by Warayut Poomiwatracanont JAN 2023

from MPU6050 import MPU6050

from os import listdir, chdir
from machine import Pin
from time import sleep_ms

mpu = MPU6050()

# List all files directory.
print("Root directory: {} \n".format(listdir()))

# Change filename and fileformat here.
filename = "{}%s.{}".format("data_logs", "txt")

# Increment the filename number if the file already exists.
i = 0
while (filename % i) in listdir():
    i += 1

# Save file in path /
with open(filename % i, "w") as f:
    cols = ["Temp", "AcX", "AcY", "AcZ"]
    f.write(",".join(cols) + "\n")
    
    while True:
        # Accelerometer Data
        accel = mpu.read_accel_data() # read the accelerometer [ms^-2]
        aX = accel["x"]
        aY = accel["y"]
        aZ = accel["z"]
        print("x: " + str(aX) + " y: " + str(aY) + " z: " + str(aZ))
    
        # Gyroscope Data
        # gyro = mpu.read_gyro_data()   # read the gyro [deg/s]
        # gX = gyro["x"]
        # gY = gyro["y"]
        # gZ = gyro["z"]
        # print("x:" + str(gX) + " y:" + str(gY) + " z:" + str(gZ))
    
        # Rough Temperature
        temp = mpu.read_temperature()   # read the device temperature [degC]
        # print("Temperature: " + str(temp) + "°C")

        # G-Force
        # gforce = mpu.read_accel_abs(g=True) # read the absolute acceleration magnitude
        # print("G-Force: " + str(gforce))
    
        # Write to file
        data = {"Temp" : temp,
                "AcX" : aX,
                "AcY" : aY,
                "AcZ" : aZ
                }
        
        push = [  str(data[k]) for k in cols ]
        row = ",".join(push)
        f.write(row + "\n")
        
        # Time Interval Delay in millisecond (ms)
        sleep_ms(100)


  • 📜打印调试信息:
    在这里插入图片描述
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用ESP8266读取MPU6050数据,你需要连接MPU6050ESP8266,然后使用I2C协议进行通信。以下是一个基本的示例代码: ``` #include <Wire.h> const int MPU_addr=0x68; // I2C address of the MPU-6050 int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ; void setup(){ Wire.begin(); Wire.beginTransmission(MPU_addr); Wire.write(0x6B); // PWR_MGMT_1 register Wire.write(0); // set to zero (wakes up the MPU-6050) Wire.endTransmission(true); Serial.begin(9600); } void loop(){ Wire.beginTransmission(MPU_addr); Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) Wire.endTransmission(false); Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L) AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L) AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L) Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L) GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L) GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L) GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L) Serial.print("AcX = "); Serial.print(AcX); Serial.print(" | AcY = "); Serial.print(AcY); Serial.print(" | AcZ = "); Serial.print(AcZ); Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53); //equation for temperature in degrees C from datasheet Serial.print(" | GyX = "); Serial.print(GyX); Serial.print(" | GyY = "); Serial.print(GyY); Serial.print(" | GyZ = "); Serial.println(GyZ); delay(1000); } ``` 在这个示例中,我们使用了Wire库来与MPU6050进行通信。在setup()函数中,我们将MPU6050从休眠状态唤醒,并初始化了串口。在loop()函数中,我们从MPU6050读取14个寄存器的数据,然后将其打印到串口上。你可以根据自己的需求修改这个示例代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值