arduino Nano和nodeMCU的I2C通信-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3351|回复: 1

arduino Nano和nodeMCU的I2C通信

[复制链接]
发表于 2019-5-25 14:12 | 显示全部楼层 |阅读模式
本帖最后由 anhezhou 于 2019-5-26 18:21 编辑

接线方式 7P~D(9(TV0`42H$[4YQ[A7W.png
nodeMCU作为主机
[mw_shl_code=arduino,true]#include <Wire.h>
void setup() {
Serial.begin(9600); /* begin serial for debug */
Wire.begin(D1, D2); /* join i2c bus with SDA=D1 and SCL=D2 of NodeMCU */
}

void loop() {
Wire.beginTransmission(8); /* begin with device address 8 */
Wire.write("Hello Arduino");  /* sends hello string */
Wire.endTransmission();    /* stop transmitting */

Wire.requestFrom(8, 13); /* request & read data of size 13 from slave */
while(Wire.available()){
    char c = Wire.read();
  Serial.print(c);
}
Serial.println();
delay(1000);
}[/mw_shl_code]
从机Nano程序:[mw_shl_code=arduino,true]#include <Wire.h>

void setup() {
Wire.begin(8);                /* join i2c bus with address 8 */
Wire.onReceive(receiveEvent); /* register receive event */
Wire.onRequest(requestEvent); /* register request event */
Serial.begin(9600);           /* start serial for debug */
}

void loop() {
delay(100);
}

// function that executes whenever data is received from master
void receiveEvent(inthowMany) {
while (0 <Wire.available()) {
    char c = Wire.read();      /* receive byte as a character */
    Serial.print(c);           /* print the character */
  }
Serial.println();             /* to newline */
}

// function that executes whenever data is requested from master
void requestEvent() {
Wire.write("Hello NodeMCU");  /*send string on request */
}

[/mw_shl_code]


发表于 2020-1-3 09:07 来自手机 | 显示全部楼层
编译通不过啊
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 12:53 , Processed in 0.083667 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表