How to Make Automatic LED using W5500 Ethernet Shield

details

Let’s make a LED to be turned on automatically whenever it detects motions.

Hardware

  • FRDM-KL25Z : mbed enabled platofrm (Pin compatible with Arduino)
  • W5500 Ethernet Shield
  • PIR Sensor
  • 1W Power LED
  • LED Driver
  • CDS Sensor

Below is the hardware schematic.

mbed-led-1

According to the schematic, mount and solder above parts on the breadboard.

mbed-led-2

Connect them with mbed platform as shown below image.

mbed-led-3

Firmware

Connect to https://developer.mbed.org/, and click “Compiler” button. If the web compiler is executed, click “New” button to create a new program.

mbed-led-4

Choose a platform that you are using and input any characters for Program Name. In this tutorial, we are using FRDM-KL25Z platform and input “Auto_led” for Program Name.

mbed-led-5

When the new program creation is done, create the new file. Input “main.cpp” for the file name and click O.K button.

mbed-led-6

When “main.cpp” file is created, click “import” button to import an mbed library.

mbed-led-7

If the loading is finished, you can see the mbed library. Please choose the mbed library and click “import” button.

mbed-led-8

Check “update all sub-libraries to the latest revision” and click “Import” button.

mbed-led-9

You can see the mbed libraries are added as below.

mbed-led-10

Let’s program the simple coding to main.cpp. This program will turn on the LED using CDS sensor and PIR sernsor. If the coding is completed, click “Compile” button. If there is not any problem in your coding, the compile is completed and a ‘bin’ file is downloaded.

auto_led_9

#include "mbed.h"
Timer t;
 
DigitalOut ledR0(LED_RED);
DigitalOut ledG0(LED_GREEN);
DigitalOut ledB0(LED_BLUE);
 
DigitalOut led0(D5);
DigitalOut led1(D6);
 
DigitalIn PIR(D8);
AnalogIn CDS(A0);
 
Serial pc(USBTX, USBRX);
 
#define PLED_ON      led0 = led1 = 1
#define PLED_OFF     led0 = led1 = 0
 
int CDS_data;
int PIR_sensor;
 
int CDS_check()
{
    CDS_data = CDS.read()*1000;
    //CDS_vol = CDS_data*3.3;
    
    if(CDS_data > 600){
        //ledG0 = 1;
        return 1;
    }
    else {
        //ledG0 = 0;
        return 0;
    }
}
 
int PIR_check()
{
    PIR_sensor = PIR;
    return PIR_sensor;
}
 
int main()
{
    ledR0 = ledG0 = ledB0 = 1;
    PLED_OFF;
    unsigned char flag = 1;
    int csd_int = 2;
    int pir_int0 = 2;
    int pir_int1 = 2;
    int pir_status = 0;
    
    pc.baud(115200);
    wait(0.5f);
    pc.printf("Hello mbed!\n\r");
    pc.printf("===========================================\n\r");
    
    while (true) {
        if(CDS_check()){
            if(csd_int != 0){
                pc.printf("Dark now.\r\n");
                csd_int = 0;
            }
            ledG0 = 0;
            flag = 1;
            while(flag){
                if(PIR_check()){
                    if(pir_int0 != 0){
                        pc.printf("The movement is detected.\r\n");
                        pc.printf("Power LED is turned on.\r\n");
                        pc.printf("===========================================\n\r");
                        pir_int0 = 0;
                    }
                    t.stop();
                    t.reset();
                    t.start();
                    pir_status = 1;
                    ledR0 = 0;
                    PLED_ON;
                }
                if(pir_status == 0) {
                    flag = 0;
                }
                if(t.read_ms() > 5000 && pir_status == 1){
                    if(pir_int0 != 1){
                        pc.printf("The movement is not detected.\r\n");
                        pc.printf("Power LED is turned off.\r\n");
                        pc.printf("===========================================\n\r");
                        pir_int0 = 1;
                    }
                    t.stop();
                    t.reset();
                    pir_status = 0;
                    ledR0 = 1;
                    PLED_OFF;
                    flag = 0;
                }
            }
        }
        else{
            if(csd_int != 1){
                pc.printf("Brighter now.\r\n");
                csd_int = 1;
            }
            
            ledG0 = 1;
            if(PIR_check()){
                if(pir_int1 != 0){
                    pc.printf("The movement is detected.\r\n");
                    pir_int1 = 0;
                }
                ledB0 = 0;
            }
            else{
                if(pir_int1 != 1){
                    pc.printf("The movement is not detected.\r\n");
                    pir_int1 = 1;
                }
                ledB0 = 1;
            }
        }
        
        //pc.printf("===========================================\n\r");
        //ledB0 = !ledB0;
        //wait(1);
    }
}

The downloaded ‘bin’ file can be programmed to the board just by copy and paste into the “MBED” disk.

mbed-led-11

 

Your code can be shared with other mbed users by clicking “Publish” button.

mbed-led-12

mbed-led-13

Below is the demonstration movie.

Ethernet Connection

We will add the Ethernet connection to the board by using W5500 Ethernet Shield.

mbed-led-ethernet-1

Create a program named “auto_LED_Ethernet” and main.cpp file. Import the mbed library and  WIZnet_Library. If you search with the key word “WIZnet”, you can find the WIZnet_Library.

mbed-led-ethernet-2

Check “Update all sub-libraries to the latest revision” and click “Import” button.

mbed-led-ethernet-3

You can see WIZnet_Library is added in the “auto_LED_Ethernet” program tree.

mbed-led-ethernet-4

We will make the code that client sends the data about LED Status, motion detection and illumination intensity to the server.

#include "mbed.h"
#include "WIZnetInterface.h"
 
#define USE_DHCP    0
 
#define PORT    5000
 
const char * IP_Addr    = "192.168.11.194";
const char * IP_Subnet  = "255.255.255.0";
const char * IP_Gateway = "192.168.11.1";
const char * Target_IP  = "192.168.11.227";
unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x12,0x34,0x56};
 
Serial pc(USBTX, USBRX);
 
//#ifdef TARGET_FRDM-KL25Z
SPI spi(D11,D12,D13);
WIZnetInterface ethernet(&spi,D10,D2);
//#endif
 
Timer t;
 
DigitalOut ledR0(LED_RED);
DigitalOut ledG0(LED_GREEN);
//DigitalOut ledB0(LED_BLUE);
 
DigitalOut led0(D5);
DigitalOut led1(D6);
 
DigitalIn PIR(D8);
AnalogIn CDS(A0);
 
#define PLED_ON      led0 = led1 = 1
#define PLED_OFF     led0 = led1 = 0
 
int CDS_data;
int PIR_sensor;
 
int CDS_check()
{
    CDS_data = CDS.read()*1000;
    return CDS_data;
}
 
int PIR_check()
{
    PIR_sensor = PIR;
    return PIR_sensor;
}
int main()
{
    ledR0 = ledG0 = 1;
    PLED_OFF;
    //PIR.mode(PullDown);
    unsigned char flag = 1;
    int csd_int = 2;
    int pir_int0 = 2;
    int pir_int1 = 2;
    int pir_status = 0;
 
    pc.baud(115200);
    wait(0.5f);
    pc.printf("Hello mbed!\n\r");
    pc.printf("===========================================\n\r");
    
    #if USE_DHCP
    int ret = ethernet.init(MAC_Addr);
    #else
    int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway);
    #endif
    
    if (!ret) {
        pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
        ret = ethernet.connect();
        if (!ret) {
            pc.printf("IP: %s, MASK: %s, GW: %s\r\n",
                      ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
        } else {
            pc.printf("Error ethernet.connect() - ret = %d\r\n", ret);
            exit(0);
        }
    } else {
        pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
        exit(0);
    }
    
    //TCPSocketServer server;
//    server.bind(PORT);
//    server.listen();
    
    //pc.printf("\nWait for new connection...\r\n");
    TCPSocketConnection client;
    
    //server.accept(client);
    //client.set_blocking(false, 0); // Timeout=0.
    //pc.printf("Connection from: %s\r\n", client.get_address());
    client.connect(Target_IP,5000);
    
    if(client.is_connected() == 1){
        pc.printf("Connected success : %s\r\n", client.get_address());
        client.send("Hello server!\r\n", 15);
        PLED_ON;
        wait(0.2f);
        PLED_OFF;
        wait(0.2f);
        PLED_ON;
        wait(0.2f);
        PLED_OFF;
    }
    else {
        pc.printf("Connected fail : %s\r\n", client.get_address());
        PLED_ON;
        wait(0.4f);
        PLED_OFF;
    }
    
    while (true) {
        //printf("%d\r\n",CDS_check());
        if(CDS_check() > 600){
            if(csd_int != 0){
                pc.printf("Now is dark.\r\n");
                client.send("Now is dark.\r\n", 14);
                csd_int = 0;
            }
            ledG0 = 0;
            flag = 1;
            while(flag){
                if(PIR_check()){
                    if(pir_int0 != 0){
                        pc.printf("The movement is detected.\r\n");
                        pc.printf("Power LED is turned on.\r\n");
                        client.send("The movement is detected.\r\n", 27);
                        client.send("Power LED is turned on.\r\n", 25);
                        pir_int0 = 0;
                    }
                    t.stop();
                    t.reset();
                    t.start();
                    pir_status = 1;
                    ledR0 = 0;
                    PLED_ON;
                }
                if(pir_status == 0) {
                    flag = 0;
                }
                if(t.read_ms() > 5000 && pir_status == 1){
                    if(pir_int0 != 1){
                        pc.printf("The movement is not detected.\r\n");
                        pc.printf("Power LED is turned off.\r\n");
                        client.send("The movement is not detected.\r\n", 31);
                        client.send("Power LED is turned off.\r\n", 26);
                        pir_int0 = 1;
                    }
                    t.stop();
                    t.reset();
                    pir_status = 0;
                    ledR0 = 1;
                    PLED_OFF;
                    flag = 0;
                    wait(0.1f);
                }
            }
        }
        else{
            if(csd_int != 1){
                pc.printf("Now is brighter.\r\n");
                client.send("Now is brighter.\r\n", 18);
                csd_int = 1;
            }
            
            ledG0 = 1;
            if(PIR_check()){
                if(pir_int1 != 0){
                    pc.printf("The movement is detected.\r\n");
                    client.send("The movement is detected.\r\n", 27);
                    pir_int1 = 0;
                }
                ledR0 = 0;
            }
            else{
                if(pir_int1 != 1){
                    pc.printf("The movement is not detected.\r\n");
                    client.send("The movement is not detected.\r\n", 31);
                    pir_int1 = 1;
                }
                ledR0 = 1;
            }
        }
        
        //pc.printf("===========================================\n\r");
        //ledB0 = !ledB0;
        //wait(1);
    }
}

Below is the demo movie.

Let’s make a LED to be turned on automatically whenever it detects motions.

Hardware

  • FRDM-KL25Z : mbed enabled platofrm (Pin compatible with Arduino)
  • W5500 Ethernet Shield
  • PIR Sensor
  • 1W Power LED
  • LED Driver
  • CDS Sensor

Below is the hardware schematic.

mbed-led-1

According to the schematic, mount and solder above parts on the breadboard.

mbed-led-2

Connect them with mbed platform as shown below image.

mbed-led-3

Firmware

Connect to https://developer.mbed.org/, and click “Compiler” button. If the web compiler is executed, click “New” button to create a new program.

mbed-led-4

Choose a platform that you are using and input any characters for Program Name. In this tutorial, we are using FRDM-KL25Z platform and input “Auto_led” for Program Name.

mbed-led-5

When the new program creation is done, create the new file. Input “main.cpp” for the file name and click O.K button.

mbed-led-6

When “main.cpp” file is created, click “import” button to import an mbed library.

mbed-led-7

If the loading is finished, you can see the mbed library. Please choose the mbed library and click “import” button.

mbed-led-8

Check “update all sub-libraries to the latest revision” and click “Import” button.

mbed-led-9

You can see the mbed libraries are added as below.

mbed-led-10

Let’s program the simple coding to main.cpp. This program will turn on the LED using CDS sensor and PIR sernsor. If the coding is completed, click “Compile” button. If there is not any problem in your coding, the compile is completed and a ‘bin’ file is downloaded.

auto_led_9

#include "mbed.h"
Timer t;
 
DigitalOut ledR0(LED_RED);
DigitalOut ledG0(LED_GREEN);
DigitalOut ledB0(LED_BLUE);
 
DigitalOut led0(D5);
DigitalOut led1(D6);
 
DigitalIn PIR(D8);
AnalogIn CDS(A0);
 
Serial pc(USBTX, USBRX);
 
#define PLED_ON      led0 = led1 = 1
#define PLED_OFF     led0 = led1 = 0
 
int CDS_data;
int PIR_sensor;
 
int CDS_check()
{
    CDS_data = CDS.read()*1000;
    //CDS_vol = CDS_data*3.3;
    
    if(CDS_data > 600){
        //ledG0 = 1;
        return 1;
    }
    else {
        //ledG0 = 0;
        return 0;
    }
}
 
int PIR_check()
{
    PIR_sensor = PIR;
    return PIR_sensor;
}
 
int main()
{
    ledR0 = ledG0 = ledB0 = 1;
    PLED_OFF;
    unsigned char flag = 1;
    int csd_int = 2;
    int pir_int0 = 2;
    int pir_int1 = 2;
    int pir_status = 0;
    
    pc.baud(115200);
    wait(0.5f);
    pc.printf("Hello mbed!\n\r");
    pc.printf("===========================================\n\r");
    
    while (true) {
        if(CDS_check()){
            if(csd_int != 0){
                pc.printf("Dark now.\r\n");
                csd_int = 0;
            }
            ledG0 = 0;
            flag = 1;
            while(flag){
                if(PIR_check()){
                    if(pir_int0 != 0){
                        pc.printf("The movement is detected.\r\n");
                        pc.printf("Power LED is turned on.\r\n");
                        pc.printf("===========================================\n\r");
                        pir_int0 = 0;
                    }
                    t.stop();
                    t.reset();
                    t.start();
                    pir_status = 1;
                    ledR0 = 0;
                    PLED_ON;
                }
                if(pir_status == 0) {
                    flag = 0;
                }
                if(t.read_ms() > 5000 && pir_status == 1){
                    if(pir_int0 != 1){
                        pc.printf("The movement is not detected.\r\n");
                        pc.printf("Power LED is turned off.\r\n");
                        pc.printf("===========================================\n\r");
                        pir_int0 = 1;
                    }
                    t.stop();
                    t.reset();
                    pir_status = 0;
                    ledR0 = 1;
                    PLED_OFF;
                    flag = 0;
                }
            }
        }
        else{
            if(csd_int != 1){
                pc.printf("Brighter now.\r\n");
                csd_int = 1;
            }
            
            ledG0 = 1;
            if(PIR_check()){
                if(pir_int1 != 0){
                    pc.printf("The movement is detected.\r\n");
                    pir_int1 = 0;
                }
                ledB0 = 0;
            }
            else{
                if(pir_int1 != 1){
                    pc.printf("The movement is not detected.\r\n");
                    pir_int1 = 1;
                }
                ledB0 = 1;
            }
        }
        
        //pc.printf("===========================================\n\r");
        //ledB0 = !ledB0;
        //wait(1);
    }
}

The downloaded ‘bin’ file can be programmed to the board just by copy and paste into the “MBED” disk.

mbed-led-11

 

Your code can be shared with other mbed users by clicking “Publish” button.

mbed-led-12

mbed-led-13

Below is the demonstration movie.

Ethernet Connection

We will add the Ethernet connection to the board by using W5500 Ethernet Shield.

mbed-led-ethernet-1

Create a program named “auto_LED_Ethernet” and main.cpp file. Import the mbed library and  WIZnet_Library. If you search with the key word “WIZnet”, you can find the WIZnet_Library.

mbed-led-ethernet-2

Check “Update all sub-libraries to the latest revision” and click “Import” button.

mbed-led-ethernet-3

You can see WIZnet_Library is added in the “auto_LED_Ethernet” program tree.

mbed-led-ethernet-4

We will make the code that client sends the data about LED Status, motion detection and illumination intensity to the server.

#include "mbed.h"
#include "WIZnetInterface.h"
 
#define USE_DHCP    0
 
#define PORT    5000
 
const char * IP_Addr    = "192.168.11.194";
const char * IP_Subnet  = "255.255.255.0";
const char * IP_Gateway = "192.168.11.1";
const char * Target_IP  = "192.168.11.227";
unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x12,0x34,0x56};
 
Serial pc(USBTX, USBRX);
 
//#ifdef TARGET_FRDM-KL25Z
SPI spi(D11,D12,D13);
WIZnetInterface ethernet(&spi,D10,D2);
//#endif
 
Timer t;
 
DigitalOut ledR0(LED_RED);
DigitalOut ledG0(LED_GREEN);
//DigitalOut ledB0(LED_BLUE);
 
DigitalOut led0(D5);
DigitalOut led1(D6);
 
DigitalIn PIR(D8);
AnalogIn CDS(A0);
 
#define PLED_ON      led0 = led1 = 1
#define PLED_OFF     led0 = led1 = 0
 
int CDS_data;
int PIR_sensor;
 
int CDS_check()
{
    CDS_data = CDS.read()*1000;
    return CDS_data;
}
 
int PIR_check()
{
    PIR_sensor = PIR;
    return PIR_sensor;
}
int main()
{
    ledR0 = ledG0 = 1;
    PLED_OFF;
    //PIR.mode(PullDown);
    unsigned char flag = 1;
    int csd_int = 2;
    int pir_int0 = 2;
    int pir_int1 = 2;
    int pir_status = 0;
 
    pc.baud(115200);
    wait(0.5f);
    pc.printf("Hello mbed!\n\r");
    pc.printf("===========================================\n\r");
    
    #if USE_DHCP
    int ret = ethernet.init(MAC_Addr);
    #else
    int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway);
    #endif
    
    if (!ret) {
        pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
        ret = ethernet.connect();
        if (!ret) {
            pc.printf("IP: %s, MASK: %s, GW: %s\r\n",
                      ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
        } else {
            pc.printf("Error ethernet.connect() - ret = %d\r\n", ret);
            exit(0);
        }
    } else {
        pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
        exit(0);
    }
    
    //TCPSocketServer server;
//    server.bind(PORT);
//    server.listen();
    
    //pc.printf("\nWait for new connection...\r\n");
    TCPSocketConnection client;
    
    //server.accept(client);
    //client.set_blocking(false, 0); // Timeout=0.
    //pc.printf("Connection from: %s\r\n", client.get_address());
    client.connect(Target_IP,5000);
    
    if(client.is_connected() == 1){
        pc.printf("Connected success : %s\r\n", client.get_address());
        client.send("Hello server!\r\n", 15);
        PLED_ON;
        wait(0.2f);
        PLED_OFF;
        wait(0.2f);
        PLED_ON;
        wait(0.2f);
        PLED_OFF;
    }
    else {
        pc.printf("Connected fail : %s\r\n", client.get_address());
        PLED_ON;
        wait(0.4f);
        PLED_OFF;
    }
    
    while (true) {
        //printf("%d\r\n",CDS_check());
        if(CDS_check() > 600){
            if(csd_int != 0){
                pc.printf("Now is dark.\r\n");
                client.send("Now is dark.\r\n", 14);
                csd_int = 0;
            }
            ledG0 = 0;
            flag = 1;
            while(flag){
                if(PIR_check()){
                    if(pir_int0 != 0){
                        pc.printf("The movement is detected.\r\n");
                        pc.printf("Power LED is turned on.\r\n");
                        client.send("The movement is detected.\r\n", 27);
                        client.send("Power LED is turned on.\r\n", 25);
                        pir_int0 = 0;
                    }
                    t.stop();
                    t.reset();
                    t.start();
                    pir_status = 1;
                    ledR0 = 0;
                    PLED_ON;
                }
                if(pir_status == 0) {
                    flag = 0;
                }
                if(t.read_ms() > 5000 && pir_status == 1){
                    if(pir_int0 != 1){
                        pc.printf("The movement is not detected.\r\n");
                        pc.printf("Power LED is turned off.\r\n");
                        client.send("The movement is not detected.\r\n", 31);
                        client.send("Power LED is turned off.\r\n", 26);
                        pir_int0 = 1;
                    }
                    t.stop();
                    t.reset();
                    pir_status = 0;
                    ledR0 = 1;
                    PLED_OFF;
                    flag = 0;
                    wait(0.1f);
                }
            }
        }
        else{
            if(csd_int != 1){
                pc.printf("Now is brighter.\r\n");
                client.send("Now is brighter.\r\n", 18);
                csd_int = 1;
            }
            
            ledG0 = 1;
            if(PIR_check()){
                if(pir_int1 != 0){
                    pc.printf("The movement is detected.\r\n");
                    client.send("The movement is detected.\r\n", 27);
                    pir_int1 = 0;
                }
                ledR0 = 0;
            }
            else{
                if(pir_int1 != 1){
                    pc.printf("The movement is not detected.\r\n");
                    client.send("The movement is not detected.\r\n", 31);
                    pir_int1 = 1;
                }
                ledR0 = 1;
            }
        }
        
        //pc.printf("===========================================\n\r");
        //ledB0 = !ledB0;
        //wait(1);
    }
}

Below is the demo movie.

COMMENTS

Please Login to comment
  Subscribe  
Notify of