Simple r4 wifi project help

I have just bought an R4 uno wifi, having previously bought the r3 starter kit and worked through some of the projects. Still a compelte novice though!

I want to control 2 or 3 LEDs via switches on the Arduino cloud, using my r4 wifi and breadboard etc.
I wonder if anyone can direct me to a very basic project that will acheive this? I need the wiring diagram, and steps on how to setup and control the switches etc via the cloud. Any help would be much appreciated.

I already successfully set up the project included in the 'get started' section on the cloud, which makes the integrated r4 wifi LED blink at different speeds and switch on or off etc - but i want to control external LEDs. Hope all that makes sense :thinking::face_with_spiral_eyes:

Hello,

Even if this is a basic code, please copy paste it here in the forum so we can have an idea of how the code works.
As of now, I never used Arduino Cloud to control things but still could you to help you locate the changes to do.

With the Arduino UNO R4 WiFi, you cannot draw more than 8 mA of current from any digital output pin, so you will need to switch the external LEDs using transistors. Here is an example:

Here is the Arduino Cloud IoT Thing sketch for the "Getting Started" project:

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Cloud Blink"
  
  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  bool led;
  bool power;
  bool mode;
  int  speed;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  pinMode(LED_BUILTIN, OUTPUT);
}

int _lastToggleMillis = millis();

void loop() {
  ArduinoCloud.update();
  if (power) {
    if (mode) {
      if (millis() - _lastToggleMillis > speed) {
        led = !led;
        _lastToggleMillis = millis();
      }
    } else {
      led = true;
    }
  } else {
    led = false;
  }
  digitalWrite(LED_BUILTIN, led);
}

/*
  Since Led is READ_WRITE variable, onLedChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLedChange()  {
  Serial.print("Led status changed:");
  Serial.println(led);
}

/*
  Since Power is READ_WRITE variable, onPowerChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPowerChange()  {
  Serial.print("Power changed:");
  Serial.println(led);
}

/*
  Since Mode is READ_WRITE variable, onModeChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onModeChange()  {
  Serial.print("Mode changed:");
  Serial.println(mode);
}

/*
  Since Speed is READ_WRITE variable, onSpeedChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onSpeedChange()  {
  Serial.print("Speed changed:");
  Serial.println(speed);
}

Why not just use appropriate current limiting resistors on the LEDs instead? Modern LEDs are plenty bright with 8 mA.

1 Like

Sounds reasonable, although we don't know if OP plans to use a single pin to control multiple LEDs...

Also, although Arduino UNO R4 WiFi datasheet says that 8 mA current draw is safe, Table 2.6 in the RA4M1 data sheet seems to suggest that the maximum permissible output current per pin may in some cases be only 4 mA.*

*Disclaimer: It is entirely possible that I have not correctly understood the table in the RA4M1 data sheet, in which case a correction/clarification would be appreciated.

@ptillisch ILY

So @ardant, what you're gonna have to do is more or less copy the mechanism that is already in place for the LED_BUILTIN

This means that you'll have to create a new Cloud Variable (let's call it "my_state") , that you will link to your switches that you'll also add on your dashboard.

Then on the code side, you have to copy/paste /adjust the pinMode instruction to set the pin you chose for your external LED as an output.

And then in the loop (right after ArduinoCloud.update() for example, you could have your new digitalWrite(your_pin_led, my_state) instruction.

However, careful with the current you're going to make go through your Arduino! Don't go over 8mA.
If you don't have the good LED (or not enough brightness with a high value resistor) then you'll need to use a transistor or whatever to give more power to the LED

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.