Wednesday, May 20, 2020

Adding Ethernet to a Pi Zero

The Pi Zero has no Ethernet port. If your project requires Ethernet you can add it using either an Ethernet module or a USB to Ethernet adapter.

At this point someone is going to question why you would want to add Ethernet to a Pi Zero. Why not just use a standard Pi which already includes the port? I guess the answer is that there are some projects where using a Pi Zero is desirable due to power consumption or size considerations but you also need the functionality of Ethernet devices. I’ve got a project in mind which I will blog about in the future (if it proves to be successful).

ENC28J60 Ethernet SPI Module

Microchip make the ENC28J60 which is a stand-Alone Ethernet Controller with SPI Interface. It is available as a cheap module which can be connected to the Pi’s SPI interface.

ENC28J60 Ethernet Module

The pin-out can vary but most modules will contain a block of 10 pins carrying the following signals :

ENC28J60 Module Pin Connector
  • VCC & Gnd
  • CLK
  • INT
  • WOL
  • MISO & MOSI
  • SCK
  • CS
  • RST

Some modules require 5V while others will work with 3.3V. Pay attention to the details when purchasing one so that you can connect it to the correct voltage. My module runs from 3.3V.

Connecting ENC28J60 to the Pi Header

The module can be connected to the Pi’s GPIO header using female-female Dupont style jumper cables.

Pi Header Module Module Pi Header
CLK INT Pin 22 (GPIO25)
WOL MISO Pin 21 (GPIO9)
Pin 19 (GPIO10) MOSI SCK Pin 23 (GPIO11)
Pin 24 (GPIO8) CS RST
Pin 17 (3.3V)
Pin 2 (5V)
VCC GND Pin 25 (Gnd)

As only 7 pins need to be connected you can connect the module to the Pi Zero using 7 cables.

Connect the ENC28J60 to your Network

Obviously you should connect the ENC28J60 module to your network using an Ethernet patch cable. I connect mine to a network switch but you could connect directly to a spare port on your router.

Given how light the Pi Zero and module are you may want to tape your cables to a surface to prevent them dragging your setup onto the floor.

Pi Zero connected to ENC28J60 Ethernet Module

You may notice in this photo that rather than use Pin 25 for Ground I used Pin 39. This was due to a faulty bit of soldering on my Pi Zero header.

Pi Zero Ethernet Software Setup

The software setup is really easy. You only need to edit the config.txt file and make two potential changes. I recommend starting with a freshly imaged SD card using the latest version of Raspbian.

The config.txt file can be edited on any computer that can read the boot partition on the SD card. Or it can be edited directly on the Pi if you have a keyboard and monitor attached.

sudo nano /boot/config.txt

Find the line:

#dtparam=spi=on

and delete the # character to leave :

dtparam=spi=on

Then add the following line :

dtoverlay=enc28j60

You should end up with a section in your config.txt looking like this:

Enable ENC28J60 overlay in config.txt

Save and exit using CTRL-X, Y and ENTER.

Power-up or Reboot

If you are working directly on the Pi you can now reboot :

sudo reboot

If you configured the SD card on a PC you can now insert it into the Pi and power-up.

Your Pi should connect to your network automatically.

Set MAC Address

Unlike most other network devices you might have used in the past the ENC28J60 has no preset MAC address. This means it is randomly generated every time the device is started. This may not bother you but could cause your router to keep allocating a different IP address. If you need a consistent MAC address you have to set it in software.

In the following example I use “b8:27:eb:00:00:01”. If you have multiple devices using this technique you should make sure they are all unique and never clash with other devices on your network.

Start by creating the following file:

sudo nano /lib/systemd/system/setmac.service

Add the following contents:

[Unit]
Description=Set MAC address for ENC28J60 module
Wants=network-pre.target
Before=network-pre.target
BindsTo=sys-subsystem-net-devices-eth0.device
After=sys-subsystem-net-devices-eth0.device
[Service]
Type=oneshot
ExecStart=/sbin/ip link set dev eth0 address b8:27:eb:00:00:01
ExecStart=/sbin/ip link set dev eth0 up
[Install]
WantedBy=multi-user.target

Save and exit using CTRL-X, Y and ENTER.

sudo chmod 644 /lib/systemd/system/setmac.service
sudo systemctl daemon-reload
sudo systemctl enable setmac.service

Reboot

With everything configured you can now reboot your Pi using:

sudo reboot

Once you are back at the command line you can check what IP and MAC addresses your Pi is using with :

ifconfig

Which should give you an output looking something like :

Pi Zero ifconfig example output

Here we can see the Pi is using the MAC address we set and has been given an IP address of 192.168.001.131 by my router.

Performance

Using the speedtest-cli utility I measured the speed of the network link to be between 3.15 and 3.7 Mbits/s. Whether this is adequate will depend entirely on the project you are building.

Pi Zero ENC28J60 speed test results.

Power & Current Requirements

Lots of people are happy using these devices powered directly from the Pi’s 3.3V pin. However the datasheet for the ENC28J60 states it can require 180mA of current when transmitting which is beyond the 50mA limit of the Pi’s onboard 3.3V regulator.

For this reason I decided to ensure my ENC28J60 was never starved of current and added a small 5V to 3.3V regulator (HK1117). This small 3-pin module is easy to connect and provided the Ethernet device with all the current it might ever need.

Pi Header HK1117 ENC28J60
Pin 2 (5V) Vin
Pin 6 (Gnd) Gnd
Vout Vcc (3.3V)

Pi Zero Ethernet Troubleshooting

While setting up my module and writing this article I came across a few issues. These were solved by taking note of the following points :

  • Double check all your connections
  • Check for loose connections
  • Be careful not to confuse CLK and SCK
  • Be careful not to confuse MISO (SO) and MOSI (SI)
  • Consider using a separate 3.3V regulator to provide adequate current

Buy an Ethernet Module

The ENC28J60 is available from lots of retailers including :

My device was described as a “Mini ENC28J60” and cost £4.

The post Adding Ethernet to a Pi Zero appeared first on Raspberry Pi Spy.



from Raspberry Pi Spy https://ift.tt/2LWdT0N

No comments:

Post a Comment

Please do not enter any spam link in the comment box.

How I channel my inner Star Trek character at work

In a recent Twitter thread , I self-identified as "some days Deanna, some days Riker." Others shared their own "Star Trek Sp...