Recent

Author Topic: Lazarus control Arduino Nano with Enc28J60  (Read 25157 times)

microguy

  • New Member
  • *
  • Posts: 49
Lazarus control Arduino Nano with Enc28J60
« on: May 15, 2017, 05:16:58 am »
This is my first time to post. Hope this is right place to post about control Arduino. I would like to use Lazarus with Arduino. My computer is window 10. I just download Lazarus and got it working. I have been use Delphi 5 for a long time.  Now I would like to learn how we can control Arduino with several LEDs and servos. I have google it but still not understand how to use. I understand we need to download serial port communication. Which one should we use? I would like to use USB port. Someone mention to use Synaser. I am not sure How do we use it.  Is there a step by step instruction how we add serial componet on Lazarus? Is there a webpage where I can download and run it? I just want to control Arduino with several LEDs at first then I will add some servos. Please don't give me the link to YouTube. I am very hard of hearing and having hard time to understand video. I am 58 years old.

dogriz

  • Full Member
  • ***
  • Posts: 126
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #1 on: May 15, 2017, 08:03:40 am »
There are a few things you need to be aware of:
Arduino Nano is Atmel Microcontroller (https://en.wikipedia.org/wiki/ATmega328), you can easily program it using C language. On Arduino website there are enough resources for start (look for some examples about Ethernet) (https://www.arduino.cc/en/Reference/HomePage)

ENC28J60 is not compatible with Arduino Ethernet Library (it is for WS5xxx), so you need other library to program Arduino as TCP client or server. Here are some resources:
http://www.geeetech.com/wiki/index.php/Arduino_ENC28J60_Ethernet_Module
https://github.com/jcw/ethercard
http://www.homautomation.org/2014/10/27/how-to-choose-the-right-library-to-add-ethernet-enc28j60-to-your-arduino/
https://github.com/ntruchsess/arduino_uip

And then you can try communicating with it using TCP (or UDP) (eg. Lazarus + LNet or Synapse).

FPC 3.2.2
Lazarus 2.2.4
Debian x86_64, arm

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #2 on: May 15, 2017, 08:38:26 am »
@dogriz

No need for C. FPC supports AVR, and afaik it can target the ATMega328 directly.
It is also possible to control an ATMega328 from any of the Lazarus supported platforms with software written for that platform.

You just have to build a ppcrossavr compiler using the GNU C toolchain for AVR to compile for the ATmega328 itself.

So:
- you can write controlling software for atmega with Lazarus on e.g., linux.mac, windows
- you can target the ATMega328 directly with an FPC cross compiler.

http://wiki.lazarus.freepascal.org/AVR_Programming
http://wiki.lazarus.freepascal.org/AVR (ignore the warning: the current state works)
« Last Edit: May 15, 2017, 08:48:45 am by Thaddy »
Specialize a type, not a var.

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #3 on: May 15, 2017, 09:11:37 am »
For serial port communication in your Lazarus application take a look at these links:
http://wiki.lazarus.freepascal.org/Hardware_Access#Serial_Communication
http://forum.lazarus.freepascal.org/index.php/topic,36523.msg245030.html#msg245030
You will find small examples to get you started, but first you need to decide which serial lib/component to use. Wiki will help you with the choice. Have in mind that Synaser is not event based. Others are. Any of them will be able to talk to Arduino over USB virtual COM port.

For developing Arduino and other ATMEL AVR microcontrollers in Pascal take a look at these links:
http://forum.lazarus.freepascal.org/index.php/topic,30960.0.html
http://forum.lazarus.freepascal.org/index.php/topic,32816.msg211887.html#msg211887
http://forum.lazarus.freepascal.org/index.php/topic,24665.msg196700.html#msg196700
http://forum.lazarus.freepascal.org/index.php/topic,29128.msg183767.html#msg183767

Using forum search would bring you much more links.

You are asking for serial communication, but your topic mentions ENC28J60 which can be used for Arduino TCP or UDP networking communication. If you decide to go that path instead then you can use Synapse, Dataport, Indy and several other networking communication libraries available for Free Pascal and Lazarus.
« Last Edit: May 15, 2017, 09:16:42 am by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

dogriz

  • Full Member
  • ***
  • Posts: 126
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #4 on: May 15, 2017, 09:27:30 am »
@Thaddy
I wasn't aware of that. Thanks for the info Thaddy.
But I'm not sure how to control ENC28J60 shield with FPC.
FPC 3.2.2
Lazarus 2.2.4
Debian x86_64, arm

microguy

  • New Member
  • *
  • Posts: 49
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #5 on: May 21, 2017, 08:03:01 am »
Avra,

Thank you for the information and links. I will try find time to work on it next week. Right now I would like to try use Ardunio UNO to make several LEDs to turn on and off from computer.

In future I would like to control four or five Arduino with single one com or something. They are about ten feet apart each other. I will use it for HO train. I would like to control about 25 turnout, several RFID,and lot of LEDs. I thought I could use IC2 but it is my understand it will not work if it is long distance. I rather not to use Wi-Fi. I was thinking to use  ENC28J60 with Nano. Can we use five ENC28J60 with Nano and switch hub to computer with FPC. If we can then how do we do it?


Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #6 on: May 21, 2017, 08:47:42 am »
@Thaddy
I wasn't aware of that. Thanks for the info Thaddy.
But I'm not sure how to control ENC28J60 shield with FPC.
Usually there is some sort of C header file available with a description of e.g. the pin layout, enumerations etc. These are easily translated to Pascal with h2pas.
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #7 on: May 21, 2017, 12:16:34 pm »
You are asking for serial communication,
Synaser (part of synapse) works. I guess the other X platforms will also work. I used the ATMega328 blink example with trunk to test both native and interfacing code on a RPi3 and Windows. I made some changes, but if anyone is interested I will post it later (nothing new, just different).
« Last Edit: May 21, 2017, 12:18:22 pm by Thaddy »
Specialize a type, not a var.

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #8 on: May 21, 2017, 06:38:21 pm »
I would like to control four or five Arduino with single one com or something. They are about ten feet apart each other.
You can control them from any computer with serial port (yes, including Raspberry Pi) if you use some single master multi slaves protocol. On electrical level it can be RS485 - either 4 wires full duplex or 2 wires half duplex. So you will need 1 RS232-RS485 convertor for your computer, and one for each Arduino. Make sure that your Arduinos have RS232 and not only USB, otherwise you will need different level convertor like TTL-RS485 or USB-RS485). On protocol level it can be MODBUS RTU or ASCII, some other protocol, or even your own if you want to keep it simple. To be able to talk to everyone, all sides need to have unified electrical connection level and talk the same protocol. There are MODBUS libraries available for FPC and as Arduino C libraries. E-Lab AvrCo Pascal compiler has it, and I think that I have seen one for mikroPascal. Your board will have different MODBUS adresses, and when your PC asks for data from board 1 with address 1, only board 1 will answer. Then you ask for data from other boards one by one.

Quote
I thought I could use IC2 but it is my understand it will not work if it is long distance.
I2C can have extended range with little electronics, but how would you interface it to PC? You would need something like BusPirate or custom hardware, so I would not recommend it. Keep I2C just for connecting Arduinos to their local sensors.

Quote
I was thinking to use  ENC28J60 with Nano. Can we use five ENC28J60 with Nano and switch hub to computer with FPC. If we can then how do we do it?
Unlike W5100 and similar, ENC28J60 is a very simple chip capable to talk just UDP. However some libraries extend it and add TCP/IP layer and other popular protocols. You can talk with ENC28J60 from E-Lab AvrCo pascal, mikroPascal and Arduino C. All boards and your master PC should be connected to a ethernet switch. Your PC would then just need to call boards one by one in  a loop and request data from them. You can use MODBUS TCP protocol for ethernet, but you have very limited resources so you might want to keep it simple and light and instead implement your own request/reply data exchange protocol.
« Last Edit: May 21, 2017, 06:42:24 pm by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #9 on: May 21, 2017, 06:51:56 pm »
I will use it for HO train. I would like to control about 25 turnout, several RFID,and lot of LEDs.
OK, if by HO train you mean model train, then I have made a little research and found that DCC is a standard for model railways. There are even several DCC implementations for AVRs so you might want to take that route if you don't want to be a lone island and be able to take benefit of already existing DCC software and hardware (hint: type dcc railway on ebay).
http://opendcc.sourceforge.net/
http://mrrwa.org/
http://www.dccwiki.com/DCC_Software
http://forum.lazarus.freepascal.org/index.php/topic,24388.15.html
« Last Edit: May 21, 2017, 07:07:49 pm by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

microguy

  • New Member
  • *
  • Posts: 49
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #10 on: May 28, 2017, 11:58:53 pm »
I am lost. Right now I want to start begin to make Lazarus working with Arduino Mega on USB. I just want to be to turn on and off on several LEDs. I don't know where to start or where we can find step by step.  Is there a tutorial where I can lean how to use LAzarus with Arduino Mega?

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #11 on: May 29, 2017, 02:43:25 am »
I am lost.
OK, then to get some confidence do the most simple thing first. Connect led to your Arduino using proper resistor. Light a led on and off using Arduino IDE for writing a very simple C program. Flash it to your Arduino and verify that the led blinks. After that use Arduino IDE to write another C program that listens to serial port to change led state with serial "on" and "off" commands. To test it use some terminal software, connect to proper serial port (check that port speed and other settings match Arduino port settings), and type on and off commands. Verify that Arduino led obeys your commands. For all this you can find a nice tutorial here:
http://www.chrisbrand.co.za/2013/06/30/arduino-serial-port/

All you need to do now is to make a Lazarus application that will send serial commands to Arduino like you did previously by hand with your terminal.

Is there a tutorial where I can lean how to use Lazarus with Arduino Mega?
There is not such tutorial that I am aware of, but your question can be split into two parts:
1) Make Arduino part. That's what I have just shown you how to do.
2) Make Lazarus part. This follows...

For Lazarus part make a form with 2 buttons, include Synaser unit and add Synapse package as a new requirement to your project. In OnClick event of both buttons put something like this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   ser: TBlockSerial;
  4. begin
  5.   ser := TBlockSerial.Create;
  6.   try
  7.     ser.Connect('COM1'); // write here your USB virtual COM port number
  8.     Sleep(250);
  9.     ser.config(9600, 8, 'N', SB1, False, False);
  10.     ser.SendString('on' + #13#10); // button 2 should have 'off' here
  11.   finally
  12.     ser.free;
  13.   end;
  14. end;

Compile. Enjoy. Have fun...  ::)
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

jb007

  • Full Member
  • ***
  • Posts: 145
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #12 on: May 29, 2017, 09:07:04 pm »
Nice topic!

Very understandable and clear!

to DIY or not to DIY

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #13 on: May 30, 2017, 10:40:38 am »
Nice topic!

Very understandable and clear!
You are so kind, so I felt motivated and created an initial Arduino wiki page:  ;)
http://wiki.lazarus.freepascal.org/Arduino

Everyone feel free to expand and improve it.  ::)
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

microguy

  • New Member
  • *
  • Posts: 49
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #14 on: June 24, 2017, 07:56:26 am »
Avra,

Sorry for taking so long to respond. I was travel a lot for my company. I fiinlly be home few days. Anyway I got Arduino IDE working with Arduino Mega to turn on and off LED. Now I trying to get small Lazarus program that I create a button for one and another button for off. I think I am not sure how to use serial code and where do we put in Lazarus. Is there somewhere I can get more tutor on serial port on Lazarus or where I can download and already have source code and exe file that I can run?

Reid

 

TinyPortal © 2005-2018