I have a problem transferring data via SPI between two Arduinos. I try to send temperature, humidity and current from one Arduino (master) to another Arduino (slave) and display them on an LCD.
Master Serial monitor:Temperature: 28.00, Humidity: 69.00, Current: 2.49
Temperature: 28.00, Humidity: 69.00, Current: 2.48
Slave serial monitor:Received Temperature: 0.00, Humidity: ovf, Current: 0.00
Received Temperature: nan, Humidity: nan, Current: nan
Received Temperature: 0.00, Humidity: 0.00, Current: 0.00
Basically, in my project I read the data from the 2 sensors using Arduino master, then transmit them through spi to arduino slave which transmits them through i2c to lcd and displays them. What's the problem?
for the parasitic characters that appear on the LCD, I should connect two resistors to the sda and scl pins of the i2c, I suspect. Regarding the power supply, if I also connect ground to ground between master and slave, it will be necessary to power both Arduinos with a USB cable or will it be enough to feed only one of the 2 arduino?
I understand, thanks for the advice.
I think there is a problem in the slave code as well considering what is displayed on the slave serial monitor. I think the master code is fine considering what it transmits through the serial monitor. In the slave code I think the problem would be in the way the slave receives the data from the master via spi and how it transfers it to the lcd via i2c.
I also connected the master to the slave and for nothing on the display I still see 0 for the 3 parameters, what could I do?
makes me think that the slave code is to blame
With this slave code on serial monitor I display:Received Temperature: 0.00, Humidity: 0.00, Current: 0.00
Received Temperature: 0.00, Humidity: 0.00, Current: 0.00
On the LCD displays all 3 parameters with value 0
because this practical part will be part of a diploma thesis that should highlight the study and the way serial communication interfaces work
.I think the practical realization is a bit simplistic, in essence I only have 2 Arduinos, an LCD, i2c, spi, acs712 and dht11, but it should be more complex.
Try the following sketches (compiled and NOT tested) which are made based on post #69@J-M-Lof this thread. You may ask any question relating to these sketches and SPI Protocol.
Master Sketch:
// SENDER CODE
#include <SPI.h>
#include <DHT.h>
#include <Adafruit_Sensor.h>
#define DHTPIN A1
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
struct __attribute__((packed, aligned(1))) myData
{
float temperature;
float humidity;
float current;
};
myData data;
const byte syncHeader[] = {0xDE, 0xAD, 0xBE, 0xEF}; // Synchronization header
byte *dataPtr = (byte*)&data;
void setup()
{
pinMode(SS, OUTPUT);
Serial.begin(115200);
SPI.begin();
digitalWrite(SS, LOW); // enable Slave Select
}
void loop()
{
for (int i = 0; i < sizeof syncHeader; i++)
{
SPI.transfer(syncHeader[i]);
delayMicroseconds(1000); // adjust time against the time the ISR needs on the other side to process the byte
}
data.temperature = dht.readTemperature();
data.humidity = dht.readHumidity();
data.current = analogRead(A0) * (5.0 / 1023.0);
// Send sensor data
for (int i = 0; i < sizeof(data); i++)
{
SPI.transfer(dataPtr[i]);
delayMicroseconds(1000); // adjust time against the time the ISR needs on the other side to process the byte
}
delay(1000);
}