Arduino Ethernet Shield – Control an LED from PC

Ethernet Shield

Ethernet Shield allows an Arduino to to connect to the internet and to read and write a microSD card. This shield use Wiznet W5100 ethernet chip. Inside this chip, there are PHY, MAC, IP, and TCP layer. The advantage of using this shield over the ENC28J60 is the TCP/IP stack is already implemented by hardware on this chip. With the ENC28J60 chip the TCP/IP must implemented on the microcontroller that is interfaced to it. In this tutorial, I will explain how to control an LED from PC using this shield.

Ethernet shield

This shield is also has a microSD card socket to interfacing with a microSD card. Arduino communicates with both W5100 and microSD card using the SPI bus. This is on digital pins 13 (SCK), 12 (MISO), 11 (MOSI), 10 (SS for the W5100), and 4 (SS for the microSD card). To make a program using this shield is very easy, because there is a library from Arduino both for Ethernet and SD card.

Ethernet LED Control

Example Code

To begin make code for control LED, we must add SPI and Ethernet library. To make an LED can be controlled from PC, we will program the Arduino as a server. The Arduino server will wait for the connection from cilent. The client will send commands to the Arduino via port 23 of TCP port numbers. For making Arduino as a server we need 3 parameter: MAC address, IP address, and port number. After that, we must make server object and string variable called commandStr for storing command from client.

In the setup function, we initialize digital pin 2 as an output for LED and also initialize the ethernet and the server. The serial port here is used for debugging. In the loop function, we wait for the client. If there is a client connected and send command to Arduino, we read every character that the client sends and store that command in commandStr variable until new line character.

If a command has received, then we need to process that command. I define 3 commands that the PC can send to the Arduino: “led=0” for turn off the LED, “led=1” for turn on the LED, and led=? for querying the status of LED.

To test the Arduino server, we need a TCP client program on the PC side. I use Hercules for the TCP client program. In the Hercules, you must write the server IP and port number and then connect to the Arduino server. To send a command for turn on the LED we type led=1<LF> on Hercules. The <LF> means line feed or new line.


This is the result for this project. You can also build a simple program to control the LED, instead of just using Hercules. For example, you can use this program that I write using Java.