TCP Communications between a PC and an Arduino Nano Using Lazarus / Free Pascal

OK. I’m getting close to done with all of the combinations and permutations of ways to communicate between PC’s, Raspberry Pi’s, and Arduinos.

Today I’m going to look at using TCP on an Arduino Nano because it is a special case.

This post builds on the prior post TCP Communications between a PC and an Arduino Using Lazarus / Free Pascal.

The Nano Ethernet shield is a very nice little package:


You simply snap your Arduino Nano into the shield. The resulting package integrates into a project quite nicely:


You can purchase Ethernet shields for the Nano on eBay quite inexpensively. Looks like the going price at time of writing is about $11:

The Ethernet shield for the Nano uses a different Ethernet chip than the Uno’s Ethernet shield. The Nano uses the ENC28J60 while the Uno uses the WIZ5100. This means you must use different drivers. The normal <Ethernet.h> include file WILL NOT WORK.

I found this out the hard way when I got my first Nano Ethernet shield. I spent a lot of time learning new drivers the first time I tried this.

There are three different drivers for the ENC28J60 you can choose between. There is a nice explanation of these drivers here:

Prior to this experiment I had used the Ethershield library mentioned in the above article. To be honest, I had a lot of trouble with it. It was several years ago, so perhaps the bugs have been worked out. In looking at my old notes I noted that the driver did not do a good job taking edge conditions into account and I had so much trouble with TCP I gave up and just used UDP.

For this experiment, I used the UIPEthernet library. This was recommended to me when I had issues with Ethershield, but I was already close to done with that project. I decided to use the UIPEthernet library for this experiment partially because of past problems but also because UIPEthernet is advertised as being functionally very close to that of the Uno’s <Ethernet.h> library. And it would be nice not to have to rewrite my own code to take advantage of the ENC28J60.

The UIPEthernet Library can be found here:

In today’s experiment I took the hardware already built and moved it from the Uno to the Nano. All pins end up staying the same:


I then altered the Arduino sketch to remove the Ethernet.h library and use the UIPEthernet.h library instead:

#include                                 <UIPEthernet.h>

I then simply recompiled the Uno’s version of my program and uploaded it to the Nano. It worked on the first try.


I did have one major headache I should mention though it probably won’t impact anyone else. I usually use Code::Blocks for Arduino rather than the Arduino IDE. I, admittedly, never use the Arduino IDE. I find it way to hard to read the text. Worst case, I use Textpad and enter the program thru that and then compile with the IDE. But most often I use Code::Blocks for Arduino.

Anyway, for some reason I will never figure out (even after wasting a good 5 hours on it), Code::Blocks refuses to link this particular program. Every once in a while Code::Blocks has an infuriating issue like this. After this problem, I’m on the fence as to whether I will continue to use Code::Blocks. I don’t program C++ for a living so when weird issues happen, I blame my code, and waste a lot of time only to discover the problem is not mine.

Last, but not least. Here is the code for this experiment.


This entry was posted in c-arduino, c-lazarus and tagged , . Bookmark the permalink.

2 Responses to TCP Communications between a PC and an Arduino Nano Using Lazarus / Free Pascal

  1. Pingback: Wiring Teensy 3.1 and ENC28J60 Ethernet Module Together | Big Dan the Blogging Man

  2. Alex Duarte says:

    Thanks for sharing your knowledge!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s