Arduino Master - Slave setup - arduino

The master board is a Arduino Uno and the slave is a Arduino Yun. I have them both wired up using A4 & A5 of one board to attach to the A4 & A5 pin on the other. They are both powered separately but share the same ground like the diagram below.
The code for both master and slave are below. If the slave board is not powered the master knows it can not talk to the slave and won't print out Loop until the slave is powered. This suggests that the master knows of the slave.
However the slave never receives this signal from the master. receiveEvent is never called.
I am doing this as I have ran out of pins on the master and want the slave to control a LCD display that takes up several pins as I'm sure you all know.
Any help would be great, thanks.
Master
#include <Wire.h>
#define SLAVE_ADDRESS 0x9
void setup() {
Serial.begin(9600);
Wire.begin();
delay(1000);
}
void loop() {
Wire.beginTransmission(SLAVE_ADDRESS);
Wire.write('T');
Wire.endTransmission();
Serial.println("Loop");
delay(1000);
}
Slave
#include <Wire.h>
#define SLAVE_ADDRESS 0x9
void setup() {
Serial.begin(9600);
delay(1000);
Wire.begin(SLAVE_ADDRESS);
Wire.onReceive(receiveEvent);
Serial.println("Setup");
}
void loop() {
// Serial.println("Loop");
}
void receiveEvent(int howMany){
Serial.println("Receive event");
while (Wire.available() > 0)
{
char c = Wire.read();
Serial.println(c);
}
}

Maybe you should think about the wiring.
Your Yun is using a different layout see: http://forum.arduino.cc/index.php?topic=191974.0
If the UNO is a R3 layout you can use the pins above AREF as far as I know. Check the exact pin-out of your Arduino(s).

Related

Sending data from Arduino UNO to NodeMCU over UART and processing received data on NodeMCU

I'm trying to send data from Arduino UNO to NodeMCU via UART.
What I want to do is that when Arduino UNO sends "on" String to the NodeMCU, NodeMCU lights up its builtin LED, when "off" - it turns off.
I send data from Arduino UNO via standard Serial.println (). On the NodeMCU I use the SoftwareSerial library. I assigned rx and tx to pins D7 and D8 accordingly. Serial ports on Arduino(standard) and NodeMCU(SoftwareSerial) are set at 9600 baud rate.
Standard Serial port (USB) of NodeMCU is set to 115200.
I send the string that I receive from the Arduino to the standard serial port of the node (connected to usb)
The question is:
On the standard port of NodeMCU, which I view through the Arduino IDE, messages coming from the arduino are displayed, and displayed correctly (those that were sent), but the NodeMCU does not want to accept them in conditional statements and light up my LED. Why?
At the same time, when I remap the virtual UART to its original pins (connected to USB, GPIO3 and GPIO1, and send the same messages via usb through the COM port view in the Arduino IDE, the LED turns on and off as I programmed it.
Do you have any ideas why this is happening?
By the way, I do not lower voltage coming from Arduino RX and TX pins from 5V to 3.3V, but since messages are recived coorectly, I don't think that this is causing a problem.
Here's my code:
Arduino:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("on");
delay(1000);
Serial.println("off");
delay(1000);
}
NodeMCU:
#include <SoftwareSerial.h>
SoftwareSerial s(D7,D8);//rx,tx
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
s.begin(9600);
pinMode(D4,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
String str = s.readStringUntil('\n');
Serial.println(str);
if(str == "on"){
digitalWrite(D4, HIGH);
}
if(str=="off"){
digitalWrite(D4, LOW);
}
}
Screenshot of COM4:
Screenshot of COM4:
UPD: I tried using sending 1 or 0 as int value via Serial.write() and s.read() and it works, maybe the prolem is in String type somehow
You Use İt Maybe Work
#include <SoftwareSerial.h>
String text = "";
SoftwareSerial s(D7,D8);//rx,tx
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
s.begin(9600);
pinMode(D4,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
String str = s.readStringUntil('\n');
text = str
Serial.println(text);
if(text == "on"){
digitalWrite(D4, HIGH);
}
if(text =="off"){
digitalWrite(D4, LOW);
}
}

Android Things to Arduino using I2C - error 6: No such device or address

I try to connect an NXP i.MX7D running Android Things to a Arduino UNO using I2C. The slave code is fairly simple :
#include <Wire.h>
void setup() {
Wire.begin(0x08); // join i2c bus with address #8
Wire.onRequest(requestEvent); // register event
}
void loop() {
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
Wire.write("hello "); // respond with message of 6 bytes
// as expected by master
}
The two devices are connected like this:
NPX SDA Pin ---> 3.3v level converter 5v ----> Arduino PIN A4
NPX SCL Pin ---> 3.3v level converter 5v ----> Arduino PIN A5
When I use the PIO tool I can't seem to connect or read the Arduino slave. There is two BUS (I2C1, I2C2) on the NPX I tried both with the same result:
imx7d_pico:/ $ pio i2c I2C1 0x08 read-reg-byte 0x00
[WARNING:client_errors.cc(35)] error 6: No such device or address
6|imx7d_pico:/ $ pio i2c I2C2 0x08 read-reg-byte 0x00
[WARNING:client_errors.cc(35)] error 6: No such device or address
I assume the level converter work properly I did have limited success with UART connection (Arduino was able to emit to NPX but not NPX to Arduino, will investigate this after)
Here are some pictures of the connection.
Try to add on Wire.onReceive callback(together with onRequest) to your arduino code.
Like this:
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}

ISP conflict on Raspberry Pi/Arduino SPI communication

I'm working on a communication between a Raspberry Pi 3+ and an Arduino Nano. The Raspberry uses ISP pins to program the Arduino. After, both should communicate in full-duplex via SPI protocol, using the WiringPi library under Qt environment. The problem is that SPI only returns 0 on both slave and master side.
Is it possible that the ISP programmer enter in conflict with the SPI protocol, even if not used at the same time?
Master code:
GpioMachine::GpioMachine(QObject *parent) :
QObject(parent){
wiringPiSetupGpio();
wiringPiSPISetup(0,1000000);
pinMode(CE0_PIN,OUTPUT);
}
void GpioMachine::sendSPI(int channel, int cmd,int arg)
{// send 2 bytes to slave
unsigned char buffer[2];
buffer[0]=cmd;
buffer[1]=arg;
wiringPiSPIDataRW(channel,buffer,2);
int val;
val=QChar(buffer[0]).toLatin1();
int val2;
val2=QChar(buffer[1]).toLatin1();
qDebug()<<"spi get:"<<val<<val2;
}
Slave code:
#include <SPI.h>
#include "pins_arduino.h"
// SPI interrupt routine
ISR (SPI_STC_vect)
{
byte c=SPDR;
Serial.println(c);
SPDR=5;// random testing value to send to master;
}
void setup()
{
Serial.begin(9600);
pinMode(MISO, OUTPUT);
pinMode(SS,INPUT);
// turn on SPI in slave mode
SPCR |= _BV(SPE);
SPI.attachInterrupt();
}
void loop()
{
delay(10);
}
Both codes gives only zeros. Any Idea?

ESP8266 to ESP8266 i2C Communication

I am trying to get my ESP8266's connect and send messages over an i2c bus. I am using a NodeMcu Development Board. Pins D1,D2 and GND are connected to each other.
The code on my master is :
#include <Wire.h>
void setup() {
Wire.begin(D1,D2); // join i2c bus (address optional for master)
Serial.begin(115200);
}
byte x = 0;
void loop() {
Wire.beginTransmission(8);
Wire.write(x); // sends one byte
Wire.endTransmission(); // stop transmitting
Serial.println("Transmitted");
x++;
delay(500);
}
And the code on my slave ESP is:
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(115200); // start serial for output
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
Serial.println("Received..");
/*
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
*/
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}
Running this gives no output on the receiver chip.
As mentioned in the comments it doesn't look like I2C is supported, but you could use PJON
You just need to connect a single wire to enable communication between the two devices
I'm not sure but I would expect the Wire library from Arduino to use the hardware I2C controller for ATMega. The I2C driver in the firmware from Espressif seems to be doing I2C over GPIO, that would hint there is no hw controller on ESP (what are the odds they would be the same anyway). So you need to use something else than Wire.h, thus I would suggest - try downloading something that fakes I2C over GPIO for your Arduino IDE. Like this .. maybe, I haven't tried that out. I know not a complete solution, but maybe at least this helps.. good luck!
ESP8266(I2C Master) to ESP8266(I2C Slave) works from version 2.5.0. Check out my comments on the ESP8266 GitHub

simple communication between AtTiny85 and Arduino (I2C)

Hi I want to connect the arduino pro mini to my AtTiny85 over I2C.
The arduino should tell the attiny to switch a LED on or off.
The arduino manages to switch the led on my attiny on but it never goes off.
I don't have any clue why?
Here is my Code for master and slave:
MASTER:
#include <Wire.h>
#define device (1)
void setup() {
// put your setup code here, to run once:
Wire.begin();
}
void loop() {
// put your main code here, to run repeatedly:
Wire.beginTransmission(device);
Wire.write(1);
Wire.endTransmission();
delay(2000);
Wire.write(0);
Wire.endTransmission();
delay(2000);
}
SLAVE:
#include <TinyWireS.h>
#include <usiTwiSlave.h>
#define output (4)
#define I2C_SLAVE_ADDR (1)
void setup() {
// put your setup code here, to run once:
TinyWireS.begin(I2C_SLAVE_ADDR);
pinMode(output, OUTPUT);
}
volatile byte msg = 0;
void loop() {
if(TinyWireS.available())
msg = TinyWireS.receive();
if(msg == 1)
digitalWrite(output, HIGH);
else if(msg == 0)
digitalWrite(output, LOW);
else
msg = 0;
}
I finally found my mistake:
when I do: Wire.write(0); I forgot to start the transmission with: Wire.beginTransmission(device);

Resources