Ethernet Communication with PIC

In this series of PIC Microcontroller Communication, after learning on how PIC Microcontroller Communication withI2C, SPI, RS232 and USB can be achieved, in this article we are going to discuss the concepts of Ethernet Communication with PIC Microcontroller. When it comes to communication involving wires, Ethernet is the leading wired standard for networking as it enables to connect a very large number of computers, microcontrollers and other computer-based equipment to one another.

With just a network switch, many different devices can easily communicate with one another with Ethernet, allowing different devices and equipment to be accessed remotely and this also provides a cost-effective and reliable means of remote control and monitoring. Most of computers nowadays have an Ethernet port implemented on them so it is with many electronic devices. Many microcontrollers have built in ethernet peripheral, like the PIC18F97J60, this PIC18 Microcontroller has an integrated 10Mbps Ethernet communications peripheral. The device offers 128K flash program memory, 3908 bytes of RAM memory, 70 I/O pins, 16 10-bit A/D channels, timers, counters and many more features.

PIC18F97J60 Family Features

  • IEEE 802.3 compatible Ethernet Controller
  • Fully Compatible with 10/100/1000 Base-T networks
  • Integrated MAC and 10 BASE-T PHY
  • Up to 128KB Flash
  • 8 KB Ethernet Buffer
  • Support for Unicast, Multicast and Broadcast packets
  • Programmable wake-up on multiple packet formats,including Magic Packet®, Unicast, Multicast, Broadcast,specific packet match or any packet
  • 64/80/100-Pin TQFP Package
  • Temperature Range: -45°C to + 85°C
  • Digital Communication Peripherals: 2-UART, 2-A/E/USART, 2-SPI, 2-I2C2-MSSP(SPI/I2C)

More information can be obtained from the PIC18F97J60 Family datasheet.

When a microcontroller which does not have an integrated ethernet peripheral is used, Microchip offer a serial Ethernet chip that can easily be used by any microcontroller with an SPI interface to provide Ethernet capability to the application. The ENC28J60 is a popular 28-pin serial Ethernet chip, 10BASE-T stand alone Ethernet Controller with SPI interface, on board MAC & PHY, 8 Kbytes of Buffer RAM and an SPI serial interface. With a small foot print package size the ENC28J60 minimizes complexity, board space and cost.

ENC28J60 Ethernet Controller Features

  • IEEE 802.3™ Compatible Ethernet Controller
  • Fully Compatible with 10/100/1000Base-T Networks
  • Integrated MAC and 10Base-T PHY
  • Supports One 10Base-T Port with Automatic Polarity Detection and Correction
  • Supports Full and Half-Duplex modes
  • Programmable Automatic Retransmit on Collision
  • SPI Interface with Clock Speeds up to 20 MHz
  • Buffer: 8-Kbyte Transmit/Receive Packet Dual Port SRAM
  • MAC address: Supports Unicast, Multicast and Broadcast Packets
  • Operating Voltage of 3.1V to 3.6V (3.3V typical)
  • Temperature Range: -40°C to +85°C Industrial, 0°C to +70°C Commercial (SSOP only)
  • 28-Pin SPDIP, SSOP, SOIC, QFN Packages

More information can be obtained from the ENC28J60 datasheet. The figures below show ENC28J60 package types

Figure 2: ENC28J60 package types.

 Embedded Ethernet applications

With internet in mind, one can think about many applications, below a few applications are listed:

  • Web-based monitoring: A person can monitor several vending machines located in different places which can be several kilometers apart. From the PC at the conform of your desk, you could monitor live every day the status and stock of each machine, without wasting time and money to travel to each machine. A simple online interface to the vending machines can allow you to monitor everything from the internet.
  • Access control: Monitor and control the access of an area from a remote location
  • Environmental monitoring: Environmental sensors can installed unattended in remote locations feeding data online to a control and monitoring center.
  • Industrial control: The status of machines could be remotely monitored, switch on or off,…
  • Home automation: Turning devices inside the house like lights ON and OFF from a remote location even from a hand held mobile device connected to internet like a smart phone.

Basics of Ethernet Communications

Before we go deep, let us discuss first some few terminology of Ethernet communication.


Ethernet is a frame-based computer networking technology for Local Area Networks (LAN). Data to be transmitted over Ethernet is divide into a stream of shorter pieces of data called frames. The international standard for Ethernet is the IEEE 802.3. any device supporting this standard can communicate with other devices over Ethernet.

Medium of communication

Cables used to connect devices in a typical Ethernet LAN topology are usually: coaxial cable (mostly in the past and rarely seen today), twisted pair wires (these cables are the most commonly used today with data speed of 100MB to 1GB per second. CAT5, CAT5e and CAT6 are some of the examples), fiber optic (used usually for longer distances or when higher speed (1GB/10GB) is needed). Ethernet can also be in the form of wireless LANs. The most common form of Ethernet is called 10Base-T and it provides a transmission speed up to 10Mbps.  Fast Ethernet or 100Base-T provides transmission speeds up to 100Mbps and Gigabit Ethernet provides even higher level of support at 1000Mbps.

CSMA/CD protocol

Devices on the Ethernet are all connected together, you could have sometimes up to 24 different devices connected to a network switch and all listening and trying to send data sometimes at the same time. To avoid collition of packets when two or more packets from different devices are sent at the same time, there is a protocol that manages this, the Carrier Sense Multiple Access with Collision Detection(CSMA/CD) protocol. The advantage of CSMA/CD is that, unlike Token Ring and Token Bus (which are other forms of network communication like Ethernet), all connection points can see each other with only one transmitting at a time to avoid any collisions. In case of a collision the transmitting nodes wait for a random time and attempt to re-transmit, hoping to avoid the collision.

Distance of Communication

The maximum distance of Communication of an Ethernet cable depends upon the speed of transmission and the type of cable used. For standard twisted pair type cables (CAT5/CAT6) operating at 10Mbps and 100Mbps, the maximum cable length is specified as 100 m. Using fiber-optic cables this length can be extended to 550 m using multi-mode fiber optic cables and over 2000 m using single-mode fiber optic cables. The ENC28J60 Ethernet Chip and the PIC18F97J60 provide speed up to 10Mbps.

Internet Protocol (IP)

This is the primary protocol used for internet for communication. Its routing function enables internetworking, and essentially establishes the Internet. It has the function to deliver packets from the source device to the destination device. To achieve this, each device has to have a unique IP address, this IP address is included in the packet headers.

The major version of IP version used today is the Internet Protocol Version 4 (IPv4), it’s a  32-bit number consisting of 4 octets. A Version 4 IP address would like: This version is still widely used today but due to the enormous growth of the Internet and the predicted depletion of available addresses, a new version of IP (IPv6), using 128 bits for the address, was developed in 1995. In this article we will use IPv4 for data communication.

Two protocols are supported under this scheme: Transmission Control Protocol (TCP) and User Datagram Protocol (UDP). TCP requires connection between sender and receiver and it checks if all packets were delivered correctly, it re-transmit them if an error occurs. applications which require reliability of data transmission use this protocol like web browsers, sending emails or file transfer. In some of the cases, it is not important to use a more reliable connection, a simpler UDP protocol can be used. UDP is commonly used in remote automation applications. This protocol offers many advantages like:

  • UDP does not establish connection before sending data, it just packages and send it.
  • UDP has only basic error checking using checksums.
  • UDP is efficient for broadcasting/multicast transmission.
  • UDP is faster, simpler and more efficient than TCP, however it is less robust than TCP.

UDP is widely used and recommended for data transfer over a network where performance and speed are more important than reliable delivery and where acknowledgement of delivery is not needed.

Network switch

A network switch is a device used in a network to connect devices together on a computer network by performing a form of packet switching. A switch receives packets from one device, looks at the destination IP address of the packets, and connect (switch) them to a corresponding port on which the destination device is connected. The figure below shows two IP Cameras, a desktop and a laptop connected to a switch.

The Microchip TCP/IP Stack 

Microchip offers a free licensed TCP/IP stack optimized for the PIC18, PIC24, dsPIC and PIC32 microcontroller families. Microchip’s TCP/IP stack includes the following key features:

  • Supported Protocols: ARP, IP, ICMP, UDP, TCP, DHCP, SNMP, HTTP, FTP, TFTP
  • Socket support for TCP and UDP
  • Secure Sockets Layer (SSL)
  • NetBIOS Name Service
  • DNS – Domain Name System
  • Ethernet Device Discovery

The Microchip TCP/IP Stack Application Note explains in detail how to use The Microchip TCP/IP Stack.

To download TCP/IP stack, go to Microchip Libraries for Applications Download Page

MikroC Pro for PIC Ethernet Libraries

The mikroC for PIC compiler contains built in Ethernet libraries that considerably simplify the process of writing a program for the microcontroller.

Ethernet PIC18FxxJ60 Library

This library is used for the PIC18FxxJ60 family of microcontrollers it supports:

  • IPv4 protocol.
  • ARP requests.
  • ICMP echo requests.
  • UDP requests.
  • TCP requests (no stack, no packet reconstruction).
  • ARP client with cache.
  • DNS client.
  • UDP client.
  • DHCP client.
  • packet fragmentation is NOT supported.

To learn more, go to the Ethernet PIC18FxxJ60 Library

SPI Ethernet ENC28J60 Library

This library is used with any microcontroller with Serial Peripheral Interface using the ENC28J60 stand-alone Ethernet controller. It supports:

  • IPv4 protocol.
  • ARP requests.
  • ICMP echo requests.
  • UDP requests.
  • TCP requests (no stack, no packet reconstruction).
  • ARP client with cache.
  • DNS client.
  • UDP client
  • DHCP client.
  • packet fragmentation is NOT supported.

To learn more, go to the SPI Ethernet ENC28J60 Library