Cov txheej txheem:

Pib Nrog I2C Sensor Interface ?? - Sib cuam tshuam Koj MMA8451 Siv ESP32s: 8 Kauj Ruam
Pib Nrog I2C Sensor Interface ?? - Sib cuam tshuam Koj MMA8451 Siv ESP32s: 8 Kauj Ruam

Video: Pib Nrog I2C Sensor Interface ?? - Sib cuam tshuam Koj MMA8451 Siv ESP32s: 8 Kauj Ruam

Video: Pib Nrog I2C Sensor Interface ?? - Sib cuam tshuam Koj MMA8451 Siv ESP32s: 8 Kauj Ruam
Video: Wb mu to tus niam tij thiab laug lo mekas lo mu ua si lav 2o 2024, Hlis ntuj nqeg
Anonim
Pib Nrog I2C Sensor Interface ?? - Sib cuam tshuam Koj MMA8451 Siv ESP32s
Pib Nrog I2C Sensor Interface ?? - Sib cuam tshuam Koj MMA8451 Siv ESP32s

Hauv qhov kev qhia no, koj yuav kawm paub txhua yam Yuav pib li cas, txuas thiab tau txais I2C ntaus ntawv (Accelerometer) ua haujlwm nrog tus tswj (Arduino, ESP32, ESP8266, ESP12 NodeMCU)

Kauj Ruam 1: Yuav Pib Li Cas Nrog I2C - Ntiaj Teb Zoo Tshaj Plaws ntawm Kev Sib Txuas IC

Arduino, ESP Series, PIC, Rasberry PI, thiab lwm yam yog txhua yam tsis txaus ntseeg. Tab sis koj ua dab tsi nrog nws ib zaug koj muaj?

Qhov zoo tshaj yog ntxiv cov sensors thiab lwm yam. Niaj hnub no ntau qhov thev naus laus zis tshiab tau siv I2C raws tu qauv kom tso cai rau lub khoos phis tawj, xov tooj, ntsiav tshuaj, lossis microcontrollers tham nrog lub ntsuas cua. Cov xov tooj ntse yuav ntse dua yog tias lawv tsis tuaj yeem tham nrog lub ntsuas qhov ntsuas kom paub seb txoj hauv kev koj lub xov tooj ntsib.

Kauj ruam 2: Txheej txheem cej luam ntawm I2C

I2C yog cov txheej txheem sib txuas ua ke, ib nrab-duplex kev sib txuas lus uas tso cai rau kev nyob ua ke ntawm ntau tus tswv thiab qhev ntawm tib lub npav. Lub tsheb npav I2C muaj ob kab: kab ntawv xov tooj (SDA) thiab lub moos txuas (SCL). Ob txoj kab xav tau rub tawm cov neeg tawm tsam.

SDA (Serial Data) - Kab rau tus tswv thiab qhev xa thiab txais cov ntaub ntawv. SCL (Serial Clock) - Kab uas nqa lub moos teeb liab. Nrog cov txiaj ntsig zoo li kev yooj yim thiab tus nqi tsim khoom qis, I2C feem ntau yog siv rau kev sib txuas lus ntawm cov cuab yeej siv hluav taws xob qis dua qhov nrug luv (hauv ib txhais taw).

Xav kawm paub ntau ntxiv txog I2C? ……

Kauj Ruam 3: Yuav Ua Li Cas Kho I²C Sensors

Ua ntej nkag mus rau txoj haujlwm, koj yuav tsum nkag siab qee qhov pib ntawm koj li Sensor. Yog li ncuav koj tus kheej khob kas fes ua ntej dhia dej hauv:)? …

Lub zog loj ntawm I2C yog tias koj tuaj yeem tso ntau lub cim rau ntawm plaub lub xov hlau zoo ib yam. Tab sis rau cov koog uas muaj ob peb qhov ua ntej ua qauv txuas nrog, koj yuav tsum tshem ob peb smd resistors los ntawm kev tawg, txwv tsis pub rub tawm ntawm lub npav yuav dhau los ua kev txhoj puab heev.

Cov ntaub ntawv twg peb xav tau los ntawm Datasheet ??

  1. Sensor ua haujlwm
  2. Pinouts thiab pins ua haujlwm
  3. Cov lus piav qhia interface (Tsis txhob hnov qab saib "I2c Chaw Nyob Xaiv Rooj")
  4. Sau npe !!

Txhua yam zoo koj yuav pom nws yooj yim tab sis Sau npe ?? REGISTERS tsuas yog qhov chaw nco hauv I²C ntaus ntawv. Cov ntsiab lus ntawm pes tsawg tus neeg sau npe muaj nyob rau hauv qhov ntsuas tau muab, thiab lawv tswj hwm lossis muaj dab tsi hu ua daim ntawv teev npe. Feem ntau ntawm cov ntaub ntawv ntawm lub sensor cov ntaub ntawv yog hais txog kev piav qhia yuav ua li cas txhua qhov kev sau npe ua haujlwm, thiab lawv tuaj yeem yog qhov lus qias neeg los nyeem vim tias cov ntaub ntawv tsis tshua muaj nthuav tawm ncaj qha.

Txhawm rau muab kev nkag siab ntawm kuv txhais li cas los ntawm qhov ntawd: Muaj ntau hom kev sau npe tab sis rau cov lus qhia no kuv yuav muab lawv faib ua ob hom dav: Tswj thiab Cov ntaub ntawv sau npe.

1) Tswj Kev Sau Npe

Feem ntau cov ntsuas hloov pauv li cas lawv ua haujlwm raws li qhov muaj txiaj ntsig khaws cia hauv cov npe tswj hwm. Xav txog kev tswj hwm sau npe ua cov txhab nyiaj ntawm On/Off keyboards, uas koj qhib los ntawm teeb tsa me ntsis rau 1 thiab tua los ntawm kev teeb tsa me ntsis mus rau 0. I²C chip-based sensors feem ntau muaj kaum ob lossis ntau qhov chaw ua haujlwm rau yam xws li me ntsis- Hom, cuam tshuam, tswj kev nyeem ntawv, qhov tob, piv txwv nrawm, txo suab nrov, thiab lwm yam.

2) Cov ntaub ntawv sau npe Tsis zoo li kev tswj hwm sau npe hauv txhab nyiaj-ntawm-hloov pauv, Kuv xav txog cov ntaub ntawv tso tawm cov npe raws li cov thawv tuav tus lej uas nyuam qhuav tshwm sim los khaws cia hauv daim ntawv binary. Yog li koj xav paub Cov Ntaub Ntawv, ib txwm nyeem cov ntaub ntawv sau npe zoo li kuv yog leej twg rau npe rau ntaus cim, Cov xwm txheej sau npe thiab lwm yam..

Yog li, pib ua I²C sensor yog txheej txheem ntau txheej txheem thiab qhov ua kom raug ntawm kev ua haujlwm feem ntau piav qhia hauv kev sau rov qab, Tsis txhob hais ncaj qha rau hauv Cov Ntawv Qhia. sau tsis hais "Yuav kom tau txais kev nyeem los ntawm lub ntsuas no, ua (1), (2), (3), (4), thiab lwm yam", tab sis koj pom cov lus piav qhia ntawm kev tswj cov npe khoom hais tias "ua ntej koj teeb tsa x hauv qhov no sau npe koj yuav tsum teeb tsa y hauv lwm qhov kev tswj hwm sau npe ".

Tseem kuv ib txwm pom cov ntawv cov ntaub ntawv sib cuam tshuam ntau dua li cov ntawv. yog tias koj yuav siv nws rau ib qho tshwj xeeb lossis ib qho ntawm cov ntaub ntawv thiab nws muab koj txhua yam ntxaws ntxaws, kev sib txuas thiab kev xa mus. Tsuas yog zaum thiab nyeem kom tawm tag nrho koj cov ntawv ua tim khawv.:)

Kauj Ruam 4: Pib Nrog Motion - Accelerometer

Cov ntsuas ntsuas niaj hnub yog Micro Electro-Mechanical Systems (MEMS) cov cuab yeej, uas txhais tau tias lawv muaj peev xwm haum rau ntawm lub nti me me sab hauv me me ntawm cov khoom siv. Ib txoj hauv kev los ntsuas kev ua haujlwm nrawm los ntawm MEMS cov ntsuas nrawm yog siv cov khoom me me uas tau muab tshem tawm ntawm cov dej ntws. Kev nrawm ntawm lub cuab yeej ua rau cov dej ntws los nthuav lossis sib cog lus, thiab qhov cuam tshuam ntawm cov khoom siv hluav taws xob tuaj yeem ntsuas tau los ntawm kev hloov pauv ntawm lub peev xwm mus rau qhov ze, cov phaj ruaj khov.

Accelerometers tau teev tseg los ntawm cov yam ntxwv hauv qab no:

  1. Tus naj npawb ntawm txoj kab, los ntawm ib mus rau peb txoj kab, sau X, Y, thiab Z hauv cov duab qhia meej. Nco tseg tias qee qhov kev ntsuas nrawm raug hu ua 6-axis lossis 9-axis, tab sis tsuas yog txhais tau tias lawv tau ntim nrog lwm cov cuab yeej MEMS xws li gyroscopes thiab/lossis magnetometers. Txhua ntawm cov cuab yeej no tseem muaj peb lub hauv paus, uas yog vim li cas thiaj muaj 3, 6, lossis 9-axis Inertial Measurement Units (IMUs).
  2. Hom kev tso tawm, xws li analog lossis digital. Ib lub ntsuas cua digital saib xyuas kev teeb tsa cov ntaub ntawv nrawm mus rau hauv cov sawv cev digital uas tuaj yeem nyeem tawm dhau I2C lossis SPI.
  3. Qhov ntau ntawm kev nrawm ntsuas hauv g's, qhov twg 1g yog kev nrawm vim lub ntiaj teb lub ntiajteb txawj nqus.
  4. Cov neeg ua haujlwm sib xyaw uas tuaj yeem tshem tawm qee qhov kev suav xav tau los tshuaj xyuas cov ntaub ntawv nyoos los ntawm MCU. Feem ntau cov accelerometers muaj qee qhov cuam tshuam cuam tshuam yooj yim txhawm rau txheeb xyuas qhov pib nrawm (poob siab) thiab 0-g (tsis poob) qhov xwm txheej. Lwm tus tuaj yeem ua tiav kev ua tiav ntawm cov ntaub ntawv nyoos los muab cov ntaub ntawv muaj txiaj ntsig ntau rau MCU.

Kauj Ruam 5: Kev Sib Txuas Nrog Tus Tswj

Txij li thaum peb paub ESP Microcontrollers hauv kev hloov pauv, peb yuav siv ESP32 rau peb qhov piv txwv. Yog li ua ntej koj xav tau Nodemcu-32s.

Tsis muaj kev txhawj xeeb yog tias koj muaj lwm ESP cov laug cam lossis txawm tias Arduino !!! Koj tsuas yog yuav tsum teeb tsa koj li Arduino IDE thiab teeb tsa raws li koj li kev txhim kho cov laug cam, rau Arduino, ESP NodeMCU, ESP32s thiab lwm yam … Koj tseem yuav xav tau qee yam ntawm I2C ntu, feem ntau ntawm pawg thawj coj tawm. Hauv qhov kev qhia no kuv yuav siv MMA8451 cov Accelerometer breakout board.

Thiab ob peb jumper xov….

Kauj Ruam 6: Kev Sib Txuas

Kev sib txuas
Kev sib txuas

Thiab ntawm no yog txheej txheem.

Kuv siv qhov txuas hauv qab no los ntawm cov qauv saum toj no rau kuv Nodemcu-32s module.

ESP32s - Qauv

3v3 - Kev

Gnd - Yog

SDA 21 - SDA

TSO 22 - SCL

"Nco ntsoov, feem ntau tsis yog txhua qhov kev txhim kho cov laug cam (feem ntau hauv ESPs) muaj qhov zoo meej pinout los pab txiav txim seb cov pins twg siv !!"

Kauj ruam 7: Code

Qhov no xav tau Adafruit lub tsev qiv ntawv

los ntawm

Rub tawm, unzip thiab koj yuav pom cov piv txwv nplaub tshev, hauv nplaub tshev tsuas yog qhib MMA8451demo hauv koj Arduino IDE thiab ntawm no koj mus ….

koj yuav pom cov cai hauv qab no rau koj li MMA8451 sensor interface nrog koj tus tswj

#suav nrog

#include #include Adafruit_MMA8451 mma = Adafruit_MMA8451 (); teeb tsa tsis muaj dab tsi (tsis muaj dab tsi) {Serial.begin (9600); Hlau.begin (4, 5); / * koom nrog i2c npav nrog SDA = D1 thiab SCL = D2 ntawm NodeMCU */ Serial.println ("Adafruit MMA8451 xeem!"); yog (! mma.begin ()) {Serial.println ("Tsis tuaj yeem pib"); thaum (1); } Serial.println ("pom MMA8451!"); mma.setRange (MMA8451_RANGE_2_G); Serial.print ("khwv ="); Serial.print (2 << mma.getRange ()); Serial.println ("G"); } void loop () {// Nyeem cov ntaub ntawv 'raw' hauv 14-ntsis suav mma.read (); Serial.print ("X: / t"); Serial.print (mma.x); Serial.print ("\ tY: / t"); Serial.print (mma.y); Serial.print ("\ tZ: / t"); Serial.print (mma.z); Serial.println (); / * Tau txais qhov xwm txheej sensor tshiab */ sensors_event_t kev tshwm sim; mma.getEvent (& kev tshwm sim); / * Muab cov txiaj ntsig (ntsuas nrawm ntsuas hauv m/s^2) */Serial.print ("X: / t"); Serial.print (event.acceleration.x); Serial.print ("\ t"); Serial.print ("Y: / t"); Serial.print (event.acceleration.y); Serial.print ("\ t"); Serial.print ("Z: / t"); Serial.print (event.acceleration.z); Serial.print ("\ t"); Serial.println ("m/s^2"); / * Tau txais kev taw qhia ntawm lub sensor */ uint8_t o = mma.getOrientation (); hloov (o) {case MMA8451_PL_PUF: Serial.println ("Portrait Up Front"); tawg; rooj plaub MMA8451_PL_PUB: Serial.println ("Portrait Up Back"); tawg; rooj plaub MMA8451_PL_PDF: Serial.println ("Portrait Down Front"); tawg; rooj plaub MMA8451_PL_PDB: Serial.println ("Portrait Down Back"); tawg; rooj plaub MMA8451_PL_LRF: Serial.println ("Toj roob hauv pes sab xis"); tawg; rooj plaub MMA8451_PL_LRB: Serial.println ("Toj roob hauv pes txoj cai rov qab"); tawg; rooj plaub MMA8451_PL_LLF: Serial.println ("Toj roob hauv pes sab laug pem hauv ntej"); tawg; rooj plaub MMA8451_PL_LLB: Serial.println ("Toj roob hauv pes Rov Qab"); tawg; } Serial.println (); ncua (1000); }

Txuag, Txheeb Xyuas thiab Tshaj Tawm ……

Qhib tus saib xyuas ntu thiab koj yuav pom qee yam zoo li no, Kuv tau txav lub sensor txog qhov no ntau yam nyeem

X: -2166 Y: 1872 Z: 2186

X: -2166 Y: 1872 Z: 2186X: -4.92 Y: 5.99 Z: 4.87 m/s^2

Toj roob hauv pes sab laug

X: -224 Y: -2020 Z: 3188

X: -5.10 Y: -3.19 Z: 7.00 m/s^2

Portrait Up Pem Hauv Ntej

Zoo yog txhua yam mus raws li nws yuav tsum tau, tam sim no koj muaj cov hauv paus ntawm I2C thiab Yuav txuas koj lub cuab yeej li cas..

Tab sis ntaus ntawv tsis Ua Haujlwm ??

Tsuas yog mus nrog cov kauj ruam tom ntej ……

Kauj Ruam 8: Tau Txais Koj I2C Device Ua Haujlwm

Cov kauj ruam yooj yim kom tau txais I2C cov cuab yeej ua haujlwm

Cia peb tshawb nrhiav….

  • Kev txuas yog qhov raug.. (xyuas nws dua)
  • Qhov kev zov me nyuam yog qhov raug.. (Yog, nws yog xeem piv txwv..)

Pib nrog cov theem los daws teeb meem ….

Theem 1: Khiav I2C cov cuab yeej ntsuas lub tshuab txhawm rau txheeb xyuas qhov chaw nyob ntawm ntaus ntawv thiab ua ntej koj lub cuab yeej I2C yog qhov zoo

Koj tuaj yeem rub tawm daim duab thiab txheeb xyuas qhov tso tawm.

Qhov tshwm sim - Ntaus ntawv ua haujlwm thiab Sensor chaw nyob yog qhov yog

I2C tshuab luam ntawv. Luam theej duab…

Pom qhov chaw nyob: 28 (0x1C) Ua tiav. Pom 1 lub cuab yeej.

Theem 2: Txheeb xyuas lub tsev qiv ntawv sensor

Qhib Adafruit_MMA8451.h cov ntaub ntawv thiab nrhiav chaw nyob ntaus ntawv

Qhov tshwm sim - Chaw nyob txawv ntawm kuv lub cuab yeej ??

/*======================================================== ========================= I2C ADDRESS/BITS --------------------- ----------------------------------------------------------------------- * /#define MMA8451_DEFAULT_ADDRESS (0x1D) //! <Default MMA8451 I2C address, yog A yog GND, nws 0x1C /*======================= ====================================================================== */

Ua - Kho cov ntaub ntawv los ntawm notepad (hloov chaw nyob) + Txuag + Rov pib dua IDE

Nws ua haujlwm. Koj tuaj yeem nyeem koj cov ntawv.

Tsis yog …. ???

Theem 3: Txheeb xyuas Wire.begin tau sau dua?

Qhib Adafruit_MMA8451.c cov ntaub ntawv thiab nrhiav Wire.begin.

Cov txiaj ntsig - Cov lus no tau sau tseg

/********************************************************** ****************************** //*! @brief teeb tsa HW (nyeem qhov tseem ceeb coefficients, thiab lwm yam)* / / ****************************************** ****************************************/ bool Adafruit_MMA8451:: pib (uint8_t i2caddr) {Wire.begin (); _i2caddr = i2caddr;

Ua - Kho cov ntaub ntawv los ntawm notepad (cov lus hais tawm) + Txuag + Rov pib dua IDE

Thiab thaum kawg Device tau Ua Haujlwm ☺…

Kuv yuav luag dhau qhov kev qhia no vim nws lub hom phiaj tseem ceeb yog piav qhia Yuav pib li cas, tau txais cov ntaub ntawv los ntawm cov ntawv, txuas thiab tau txais I2C lub cuab yeej ua haujlwm nrog piv txwv yooj yim heev.

Pom zoo: