Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Dhrumil Makadia
Published © CC BY

Wireless AC Power Dimmer for Fan Speed Controller Using RF

This project uses an RF module, Wemos D1 mini, Wemos D1 mini battery shield, push button, AC light dimmer, and power bank.

IntermediateProtip2 hours1,383
Wireless AC Power Dimmer for Fan Speed Controller Using RF

Things used in this project

Hardware components

Adraxx 433Mhz RSI Wireless Transmitter Receiver Module
×1
Wemos D1 mini
×2
WeMos D1 Mini Single Lithium Battery Charging And Battery Boost Shield
×2
Push Button
×3
AC Light Dimmer
×1
SAMSUNG ICR18650 2600mAh Li-Ion Battery
×2
USB Power Bank Case Charger DIY
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

RX

TX

Code

RX Code

Arduino
We have to two another files. 1). hw_timer.c 2).hw_timer.h
#include <RCSwitch.h>
#include "hw_timer.h"  
RCSwitch mySwitch = RCSwitch();
        
const byte zcPin = D3;
const byte pwmPin = D4;  

byte fade = 1;
byte state = 1;
byte tarBrightness = 255;
byte curBrightness = 0;

int val = 0;

byte zcState = 0; 
void setup() 
{
  mySwitch.enableReceive(D2);
  Serial.begin(115200);
  pinMode(zcPin, INPUT_PULLUP);
  pinMode(pwmPin, OUTPUT);
  attachInterrupt(zcPin, zcDetectISR, RISING);    // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
  hw_timer_init(NMI_SOURCE, 0);
  hw_timer_set_func(dimTimerISR);
}

void loop() 
{

  if (mySwitch.available()) 
  {
    val = mySwitch.getReceivedValue();
    if (val == 255)
    {
        int val = 255;
        tarBrightness = val;
        Serial.println(tarBrightness);
        mySwitch.resetAvailable();
    }
    if (val == 127)
    {
        int val = 127;
        tarBrightness = val;
        Serial.println(tarBrightness);
        mySwitch.resetAvailable();
    }
    if (val == 0)
    {
        int val = 0;
        tarBrightness = val;
        Serial.println(tarBrightness);
        mySwitch.resetAvailable();
    }
  }
}

void dimTimerISR() 
{
    if (fade == 1) 
    {
      if (curBrightness > tarBrightness || (state == 0 && curBrightness > 0)) 
      {
        --curBrightness;
      }
      else if (curBrightness < tarBrightness && state == 1 && curBrightness < 255) 
      {
        ++curBrightness;
      }
    }
    else 
    {
      if (state == 1) 
      {
        curBrightness = tarBrightness;
      }
      else 
      {
        curBrightness = 0;
      }
    }
    
    if (curBrightness == 0) 
    {
      state = 0;
      digitalWrite(pwmPin, 0);
    }
    else if (curBrightness == 255) 
    {
      state = 1;
      digitalWrite(pwmPin, 1);
    }
    else 
    {
      digitalWrite(pwmPin, 1);
    }
    
    zcState = 0;
}

void zcDetectISR() 
{
  if (zcState == 0) 
  {
    zcState = 1;
  
    if (curBrightness < 255 && curBrightness > 0) 
    {
      digitalWrite(pwmPin, 0);
      int dimDelay = 30 * (255 - curBrightness) + 400;
      hw_timer_arm(dimDelay);
    }
  }
}

TX Code

Arduino
#include <RCSwitch.h>         

RCSwitch mySwitch = RCSwitch();

int button1,button2,button3 = 0;

void setup() 
{
  Serial.begin(115200);   
  pinMode(D3,INPUT); //ON
  pinMode(D4,INPUT); //MID
  pinMode(D5,INPUT); //OFF
  mySwitch.enableTransmit(D6);
}

void loop() 
{
    button1 = digitalRead(D3);
    if (button1==HIGH)
    {
        int val = 255;
        Serial.println(val);
        mySwitch.send(val, 24); //24 is the bit format
        delay(100); 
    }
    
    button2 = digitalRead(D4);
    if (button2==HIGH)
    {
        int val = 127;
        Serial.println(val);
        mySwitch.send(val, 24); //24 is the bit format
        delay(100); 
    }
    
    button3 = digitalRead(D5);
    if (button3==HIGH)
    {
        int val = 0;
        Serial.println(val);
        mySwitch.send(val, 24); //24 is the bit format
        delay(100); 
    }
}

Credits

Dhrumil Makadia
40 projects • 43 followers
Contact

Comments

Please log in or sign up to comment.