Hi,
Thank you for your answer.
My setup is supplied by the USB port.
The RGB shield is set to a low brightness so it shouldn't matter.
I've investigated more and found the issue and a (quick & dirty) fix
The problem actually comes from the Wire library having the endTransmission() hanging.
As stated in this post, I've added explicit calls to ENV.begin() and MATRIX.begin() before calling their functions in the loop() method and now my sketch works as expected.
I don't really understand why those two libraries, using different protocols (ENV uses I2C & RGB uses SPI) are conflicting like this but I guess there must be an explanation somewhere...
Here is the (working) code:
/*
MKR ENV Shield - Read Sensors
This example reads the sensors on-board the MKR ENV shield
and prints them to the Serial Monitor once a second.
The circuit:
- Arduino MKR board
- Arduino MKR ENV Shield attached
This example code is in the public domain.
*/
#include <ArduinoGraphics.h> // Arduino_MKRRGB depends on ArduinoGraphics
#include <Arduino_MKRRGB.h>
#include <Arduino_MKRENV.h>
void setup() {
Serial.begin(9600);
while (!Serial);
if (!ENV.begin()) {
Serial.println("Failed to initialize MKR ENV shield!");
while (1);
} else {
Serial.println("MKR ENV shield initialized");
}
// initialize the display
if (!MATRIX.begin()) {
Serial.println("Failed to initialize MKR RGB shield!");
while (1);
} else {
Serial.println("MKR RGB shield initialized");
}
// set the brightness, supported values are 0 - 255
MATRIX.brightness(10);
// configure the text scroll speed
MATRIX.textScrollSpeed(125);
// display some short text without scrolling
MATRIX.beginText(0, 0, 0, 0, 127); // X, Y, then R, G, B
MATRIX.print("Init complete");
MATRIX.endText(SCROLL_LEFT);
Serial.println("Init complete");
delay(2000);
}
void loop() {
ENV.begin();
// read all the sensor values
Serial.println("Reading temperature");
float temperature = ENV.readTemperature();
Serial.println("Reading humidity");
float humidity = ENV.readHumidity();
Serial.println("Reading pressure");
float pressure = ENV.readPressure();
Serial.println("Reading illuminance");
float illuminance = ENV.readIlluminance();
Serial.println("Reading uva");
float uva = ENV.readUVA();
Serial.println("Reading uvb");
float uvb = ENV.readUVB();
Serial.println("Reading uvIndex");
float uvIndex = ENV.readUVIndex();
// print each of the sensor values
Serial.print("Temperature = ");
Serial.print(temperature);
Serial.println(" °C");
MATRIX.begin();
MATRIX.brightness(10);
MATRIX.textScrollSpeed(125);
MATRIX.beginText(0, 0); // use the same color as before
MATRIX.print(temperature);
MATRIX.println(" °c");
MATRIX.endText(); // SCROLL_LEFT parameter here to configure scrolling left
Serial.print("Humidity = ");
Serial.print(humidity);
Serial.println(" %");
Serial.print("Pressure = ");
Serial.print(pressure);
Serial.println(" kPa");
Serial.print("Illuminance = ");
Serial.print(illuminance);
Serial.println(" lx");
Serial.print("UVA = ");
Serial.println(uva);
Serial.print("UVB = ");
Serial.println(uvb);
Serial.print("UV Index = ");
Serial.println(uvIndex);
// print an empty line
Serial.println();
// wait 1 second to print again
delay(1000);
}