Arduino constantly going off line with simple AC dimmer sketch

Yes, compare Blynk’s W5100 sketch and yours.
It’s in the IDE but also shown below:

/**************************************************************
 * Blynk is a platform with iOS and Android apps to control
 * Arduino, Raspberry Pi and the likes over the Internet.
 * You can easily build graphic interfaces for all your
 * projects by simply dragging and dropping widgets.
 *
 *   Downloads, docs, tutorials: http://www.blynk.cc
 *   Blynk community:            http://community.blynk.cc
 *   Social networks:            http://www.fb.com/blynkapp
 *                               http://twitter.com/blynk_app
 *
 * Blynk library is licensed under MIT license
 * This example code is in public domain.
 *
 **************************************************************
 *
 * This example shows how to use Arduino Ethernet shield (W5100)
 * to connect your project to Blynk.
 * Feel free to apply it to any other example. It's simple!
 *
 * NOTE: Pins 10, 11, 12 and 13 are reserved for Ethernet module.
 *       DON'T use them in your sketch directly!
 *
 * WARNING: If you have an SD card, you may need to disable it
 *       by setting pin 4 to HIGH. Read more here:
 *       https://www.arduino.cc/en/Main/ArduinoEthernetShield
 *
 **************************************************************/

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

#define W5100_CS  10
#define SDCARD_CS 4

void setup()
{
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card

  Serial.begin(9600);
  Blynk.begin(auth);
  // You can also specify server.
  // For more options, see Boards_Ethernet/Arduino_Ethernet_Manual example
  //Blynk.begin(auth, "your_server.com", 8442);
  //Blynk.begin(auth, IPAddress(192,168,1,100), 8888);
}

void loop()
{
  Blynk.run();
}

ok I have had the SPI.h and ethernet.h libraries in my sketch and they made no difference. Why would the two define statements have any effect as this is the original code from the blynk getting started page that works just fine ???(shown below)

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

char auth[] = "YourAuthToken";

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth);  // Here your Arduino connects to the Blynk Cloud.
}

void loop()
{
  Blynk.run(); // All the Blynk Magic happens here...
}
Auth Token

Cheers again would love to get to the bottom of this!!!

Kev

My advice is to use the Blynk sketch for W5100 including disabling the SD card and add your bits.

Hi Costas,

Done that exactly as per sketch made no difference

here is a screenshot of the serial monitor as you can see takes for ever to connect then after a couple of goes of the slider it disconnects and then reconnects and so on…

have you disabled SD card.

2.5 minutes to get connected is not good and this suggests a problem at your side.

Is your slider on V21 set as ON for SEND ON RELEASE?
Do you have any other widgets in your project?

hi costas

thanks for your help here

yes disabled sd card yes no v21 set as off so fades as it slides.

still dont under stand though why the basic test sketch loads instantly and works perfect for weeks on end with out a singles disconnect - the ONLY difference is my code added to basic sketch???

tearing hair out!!!

cheers

kev

Probably your problem. Try it ON not OFF.

Haven’t tried this yet Costas but I have made a bit of a discovery. If I switch the mains of to the dimmer it stops crashing ??? the circuit is very standard and the only connection to the mains that involves the arduino is D2 which is interrupt 0, which in turn is read and does the zero crossing detect part of the circuit. SO, it would appear that it something to do with this??? Again at least we can tell it’s isolated to a bit of code reading pin 2 but why on earth does it matter if pin2 detects zero crossing or not??? I guess it’s because it wont be calling the zero_cross_int procedure as there is no power on so is it somethinh within this loop that is causing the issue ???

Cheers

kev

Does it still take 2.5 minutes to connect to Blynk with the dimmer power off?

yes it does and so does the blynk test sketch but both now stay online once connected???

I have also been noticing similar issues with involving delayMicroseconds() to control a servo (also on an UNO R3) while using Blynk… Where the same sketch works perfectly outside of Blynk. I came up with a band-aid fix using Blynk.syncVirtual() but that only worked if the delayMicroseconds() call was in a Blynk Loop… and it brought other issues, so not really recommended :wink:

As my Ethernet shield died awhile ago, I have been using Blynk’s USB link, and thought that might have been part of the issue, but now I think not so much :slight_smile:

Basically I suspect delayMicroseconds() might be getting interrupted at times due internal Blynk timing.

Also, while you don’t appear to be using the analog pins, a known issue with some Ethernet shield clones was that the REF pin was shorted to either ground or 5V and thus caused unreliable analog reads. I had solved that issue by just bending the REF pin out of the way.

We use delayMicroseconds() with our Blynk connected 433MHz RF transmitters. Never had a problem but we don’t use Arduinos.

Exactly… may be limited to the core board libraries.

There were timer or interrupt conflicts that have since been resolved as of Arduino 0018, but who knows what else lurks deep in the core :slight_smile:

Either way, there seems to be a clear issue involveing Arduino (at least the UNO so far) and delayMicroseconds() that cause timing/dropout issues in Blynk. Forgoing use of all arduinos for ESP’s may be a solution (hint taken :wink: ) but not ideal for everyone.

Ok… Let’s go
Test this code with your hardware (Uno R3 and ethernet shield W5100)

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include  <TimerOne.h>          // Avaiable from http://www.arduino.cc/playground/Code/Timer1
volatile int i=0;               // Variable to use as a counter volatile as it is in an interrupt
volatile boolean zero_cross=0;  // Boolean to store a "switch" to tell us if we have crossed zero
int AC_pin = 3;                // Output to Opto Triac
int brightness = 128;           // Dimming level (0-128)  0 = on, 128 = 0ff
int freqStep = 75;    // This is the delay-per-brightness step in microseconds.

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxx";

#define W5100_CS  10
#define SDCARD_CS 4


void setup()
{
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
  pinMode(AC_pin, OUTPUT);                          // Set the Triac pin as output
  attachInterrupt(0, zero_cross_detect, RISING);    // Attach an Interupt to Pin 2 (interupt 0) 
  Timer1.initialize(freqStep);                      // Initialize TimerOne library 
  Timer1.attachInterrupt(dim_check, freqStep);   
  
  
  Serial.begin(9600);
  Blynk.begin(auth);

}

BLYNK_WRITE(V1)//      slider brillo
{
 int brillo = param.asInt(); 
 brightness=brillo;
}

void zero_cross_detect() {    
  zero_cross = true;               
  i=0;
  digitalWrite(AC_pin, LOW);       // turn off TRIAC (and AC)
}
// Turn on the TRIAC at the appropriate time
void dim_check() {                   
  if(zero_cross == true) {              
    if(i>=brightness) {                     
      digitalWrite(AC_pin, HIGH); // turn on light       
      i=0;  // reset time step counter                         
      zero_cross = false; //reset zero cross detection
    } 
    else {
      i++; // increment time step counter                     
    }                                
  }                                  
}                               


void loop()
{
  Blynk.run();
}

Serial output:

[0] Getting IP...
[6484] IP:xxx.xxx.x.xx
[6486] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.4 on Arduino Uno

[6596] Connecting to xxx.xxx.xxx.xxx
[6838] Ready (ping: 4ms).

Dashboard: http://tinyurl.com/zn8rqyg

It works like a charm…

OK, blond moment here… as I left my TRIAC board in my other jacket pocket, I was going to monitor the output on my DSO (AKA little toy oscilloscope)… and was getting very strange readings until it dawned on me what zero-crossing detection meant (Ah Hah! those YouTube videos I watch all the time are starting to pay off).

So, no Ethernet shield, no TRIAC and no sine wave generator… I am out of this game :stuck_out_tongue:

But while digging through the code and looking at the picture, I notice you have something going to PIN 2 and I am guessing that is the actual interrupt pin? or is Pin 3 actually used for both functions? (I don’t fully understand how to use interrupts yet - no judging :slight_smile: )

Oh… Sorry, it’s Pin 2 in the Arduino Uno, the original code is for a Pro Micro board and for the Pro Micro the Int 0 is Pin 3…

Comment changed at the other post!

Thanks @Gunner!

Hi Psoro,

I don’t need to try this code as I know it works perfectly on my setup(which is what I stated in my original post that I had another slightly more complex sketch that works fine). I will resort to this if all else fails, but the point of my post was to find the issue that is causing this problem so I can avoid it in future and help others.

Out of interest which boards do you tend to use for these types of applications with Blynk

Cheers Kev

PS sorry for the late reply - it wouldnt let me reply for 2 hours as it was my first day being a newbie and all that lol!!!

That’s fine… the point was clear… and to be honest I didn’t pay too much attention to your code as, according your comments, it doesn’t work properly…That’s why I offered you the piece of code that works fine with or without Blynk…
I prefer to play with stuff that works and change it according my project doing it bigger and bigger instead of keep struggling with faulty code…

I used to play with Arduino and the ESP-01 as shield at the beginning and, after several time, I moved to ESP’s like WeMos Mini D1 or NodeMCU… A lot of fun here with Blynk.

Kind regards!

I agree… sometimes finding the root issue is just as important as an finding an alternative way.

I think it just comes down to the angle of the tongue at times :slight_smile: … That and competing and/or conflicting timer/interrupt issues withing Blynk libraries and Aduino core… at least when using certain methods (and apparently UNO’s :wink: ) that were not really intended for most IoT applications; of which Blynk is.

Yeah agree Gunner and thanks to Psoro for all of his help. I guess it is down the delay and issue and I will just avoid those in future. I did do a lot of reading today on the arduino site and it does mention a lot of issues with interrupts and delays, and as you pointed out is this is fixed in 018 onwards.

I will park it there guys. Thanks for all your help and lets hope it does get fixed good and proper some day!!

Cheers

kev

1 Like