Difference between revisions of "Arduino"

From Free Pascal wiki
Jump to navigationJump to search
m
m (→‎Minimal Lazarus Synaser serial example: Fixed syntax highlighting)
(19 intermediate revisions by 6 users not shown)
Line 1: Line 1:
This article describes how can your Lazarus/FPC application communicate with [https://en.wikipedia.org/wiki/Arduino Arduino boards].
+
{{Arduino}}
  
 +
{{Platform only|Arduino|Arduino|Arduino boards}}
 +
 +
Many [https://en.wikipedia.org/wiki/Arduino Arduino] projects have the need to send data to a PC running a program for data aquisition and visualisation. Lazarus is highly suitable for developing such programs, i.e. to build the PC-sided counterpart of an Arduino project.
 +
 +
For Lazarus developers Arduino opens a whole new world of smart physical sensors and actuators for monitoring, control and data acquisition.
 +
 +
The fpc compiler is even capable to compile programs for Arduino boards itself.
 +
 +
 +
[[File:Arduino_Uno_R3.png]]
 +
 +
= FPC on Arduino =
 +
Currently (2017) most Arduino boards carry microcontrollers of the AVR family (Atmega). An other popular arduino board carries a STM32 (ARM Cortex). For both microcontroller families there are fpc compilers available. Programming Arduino with FPC of course doesn't mean to use the arduino ecosystem with just a different language. Nevertheless arduino libs (C++) can be used in fpc programms, but mixing languages obviously is an advanced topic.
 +
While compilation the fpc creates a .hex-file (binary), this can be loaded to the microcontroller, arduino board respectively, using a programming software by choice. AVRDude e.g. also supports program upload using the Arduino bootloader.
 +
 +
There are further wiki pages for programing AVR microcontrollers:
 +
[[AVR]], [[AVR Programming]]
 +
 +
There is also a tutorial on how to use Lazarus on a AtMega328p (Arduino Uno/Nano) in German language:
 +
[[AVR Embedded Tutorial/de]]
 +
 +
 +
= Communication between a Lazarus application and Arduino =
 +
 +
The following section describes how your Lazarus/FPC application can communicate with Arduino boards, no matter whether fpc or arduino is running on the arduino board.
  
 
== Serial communication ==
 
== Serial communication ==
Line 9: Line 34:
  
 
==== Minimal Lazarus Synaser serial example ====
 
==== Minimal Lazarus Synaser serial example ====
We will use [http://synapse.ararat.cz/doku.php Synaser library] in this example. Create Lazarus application with one form and two 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:
 
  
<syntaxhighlight>
+
We will use [http://synapse.ararat.cz/doc/help/synaser.html Synaser] from [http://www.ararat.cz/synapse/doku.php Synapse library] in this example. Create Lazarus application with one form and two buttons. Include Synaser unit and add [[Synapse|Synapse package]] as a new requirement to your project (you do this in Project Inspector). In OnClick event of both buttons put something like this:
 +
 
 +
<syntaxhighlight lang="pascal">
 
procedure TForm1.Button1Click(Sender: TObject);
 
procedure TForm1.Button1Click(Sender: TObject);
 
var
 
var
Line 25: Line 51:
 
     ser.free;
 
     ser.free;
 
   end;
 
   end;
end;</syntaxhighlight>
+
end;
 +
</syntaxhighlight>
  
 
=== Arduino side serial application ===
 
=== Arduino side serial application ===
 
You can program your Arduino in several languages.
 
You can program your Arduino in several languages.
  
==== Minimal Arduino C sketch example ====
+
==== Minimal Arduino C sketch serial example ====
 
You can follow [http://www.chrisbrand.co.za/2013/06/30/arduino-serial-port/ this] nice step by step tutorial. In short, you need to add a led with proper resistor to your input pin 13, compile this sketch in your [https://www.arduino.cc/en/Main/Software Arduino IDE], and download it to your board:
 
You can follow [http://www.chrisbrand.co.za/2013/06/30/arduino-serial-port/ this] nice step by step tutorial. In short, you need to add a led with proper resistor to your input pin 13, compile this sketch in your [https://www.arduino.cc/en/Main/Software Arduino IDE], and download it to your board:
<syntaxhighlight>
+
<syntaxhighlight lang= cpp>
 
int led = 13; // Pin 13
 
int led = 13; // Pin 13
  
Line 53: Line 80:
 
         input += (char) Serial.read(); // Read in one char at a time
 
         input += (char) Serial.read(); // Read in one char at a time
 
         delay(5); // Delay for 5 ms so the next char has time to be received
 
         delay(5); // Delay for 5 ms so the next char has time to be received
    }
 
  
    if (input == "on")
+
        if (input == "on")
    {
+
        {
        digitalWrite(led, HIGH); // on
+
            digitalWrite(led, HIGH); // on
    }
+
        }
    else if (input == "off")
+
        else if (input == "off")
    {
+
        {
        digitalWrite(led, LOW); // off
+
            digitalWrite(led, LOW); // off
 +
        }
 
     }
 
     }
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==== Minimal Arduino mikroPascal example ====
+
==== Minimal Arduino mikroPascal serial example ====
 
[https://www.mikroe.com/mikropascal/ mikroPascal for AVR] can be used to develop Arduino programs. TBD.
 
[https://www.mikroe.com/mikropascal/ mikroPascal for AVR] can be used to develop Arduino programs. TBD.
  
==== Minimal Arduino E-Lab AvrCo example ====
+
==== Minimal Arduino E-Lab AvrCo serial example ====
 
[http://e-lab.de/AVRco/index_en.html E-Lab AvrCo Multitasking Pascal] can be used to develop Arduino programs. TBD.
 
[http://e-lab.de/AVRco/index_en.html E-Lab AvrCo Multitasking Pascal] can be used to develop Arduino programs. TBD.
  
==== Minimal Arduino FPC example ====
+
==== Minimal Arduino FPC serial example ====
 
Yes, your Arduino 8-bit AVR board [[AVR Programming|can be programmed with FPC]] too. TBD.
 
Yes, your Arduino 8-bit AVR board [[AVR Programming|can be programmed with FPC]] too. TBD.
  
Line 78: Line 105:
 
Here we show how to communicate with your board through ethernet communication channel.
 
Here we show how to communicate with your board through ethernet communication channel.
  
=== Lazarus ethernet application ===
+
=== Lazarus side ethernet application ===
 
TBD
 
TBD
  
=== Arduino ethernet application ===
+
=== Arduino side ethernet application ===
 
TBD
 
TBD
  
Line 96: Line 123:
  
 
[https://bigdanzblog.wordpress.com/2014/11/19/tcp-communications-between-a-pc-and-an-arduino-nano-using-lazarus-free-pascal/ FPC application talking to Arduino Nano over TCP]
 
[https://bigdanzblog.wordpress.com/2014/11/19/tcp-communications-between-a-pc-and-an-arduino-nano-using-lazarus-free-pascal/ FPC application talking to Arduino Nano over TCP]
 
[[Category:Embedded]]
 
[[Category:Operating Systems and Platforms]]
 
[[Category:Arduino]]
 
[[Category:AVR]]
 

Revision as of 07:42, 11 January 2020

Deutsch (de) English (en) español (es)

Arduino Logo.svg

This article applies to Arduino boards only.

See also: Multiplatform Programming Guide

Many Arduino projects have the need to send data to a PC running a program for data aquisition and visualisation. Lazarus is highly suitable for developing such programs, i.e. to build the PC-sided counterpart of an Arduino project.

For Lazarus developers Arduino opens a whole new world of smart physical sensors and actuators for monitoring, control and data acquisition.

The fpc compiler is even capable to compile programs for Arduino boards itself.


Arduino Uno R3.png

FPC on Arduino

Currently (2017) most Arduino boards carry microcontrollers of the AVR family (Atmega). An other popular arduino board carries a STM32 (ARM Cortex). For both microcontroller families there are fpc compilers available. Programming Arduino with FPC of course doesn't mean to use the arduino ecosystem with just a different language. Nevertheless arduino libs (C++) can be used in fpc programms, but mixing languages obviously is an advanced topic. While compilation the fpc creates a .hex-file (binary), this can be loaded to the microcontroller, arduino board respectively, using a programming software by choice. AVRDude e.g. also supports program upload using the Arduino bootloader.

There are further wiki pages for programing AVR microcontrollers: AVR, AVR Programming

There is also a tutorial on how to use Lazarus on a AtMega328p (Arduino Uno/Nano) in German language: AVR Embedded Tutorial/de


Communication between a Lazarus application and Arduino

The following section describes how your Lazarus/FPC application can communicate with Arduino boards, no matter whether fpc or arduino is running on the arduino board.

Serial communication

Here we show how to communicate with your board through serial communication channel.

Lazarus side serial application

There are many ways for serial communication in Lazarus and FPC.

Minimal Lazarus Synaser serial example

We will use Synaser from Synapse library in this example. Create Lazarus application with one form and two buttons. Include Synaser unit and add Synapse package as a new requirement to your project (you do this in Project Inspector). In OnClick event of both buttons put something like this:

procedure TForm1.Button1Click(Sender: TObject);
var
  ser: TBlockSerial;
begin
  ser := TBlockSerial.Create;
  try
    ser.Connect('COM1'); // write here Arduino COM port number (on linux it's something like '/dev/ttyUSB0')
    Sleep(250);
    ser.config(9600, 8, 'N', SB1, False, False);
    ser.SendString('on'); // button 2 should have 'off' here
  finally
    ser.free;
  end;
end;

Arduino side serial application

You can program your Arduino in several languages.

Minimal Arduino C sketch serial example

You can follow this nice step by step tutorial. In short, you need to add a led with proper resistor to your input pin 13, compile this sketch in your Arduino IDE, and download it to your board:

int led = 13; // Pin 13

void setup()
{
    pinMode(led, OUTPUT); // Set pin 13 as digital out

    // Start up serial connection
    Serial.begin(9600); // baud rate
    Serial.flush();
}

void loop()
{
    String input = "";

    // Read any serial input
    while (Serial.available() > 0)
    {
        input += (char) Serial.read(); // Read in one char at a time
        delay(5); // Delay for 5 ms so the next char has time to be received

        if (input == "on")
        {
            digitalWrite(led, HIGH); // on
        }
        else if (input == "off")
        {
            digitalWrite(led, LOW); // off
        }
    }
}

Minimal Arduino mikroPascal serial example

mikroPascal for AVR can be used to develop Arduino programs. TBD.

Minimal Arduino E-Lab AvrCo serial example

E-Lab AvrCo Multitasking Pascal can be used to develop Arduino programs. TBD.

Minimal Arduino FPC serial example

Yes, your Arduino 8-bit AVR board can be programmed with FPC too. TBD.

Ethernet communication

Here we show how to communicate with your board through ethernet communication channel.

Lazarus side ethernet application

TBD

Arduino side ethernet application

TBD

Resources

Forum topic that started this article

AVR

AVR Programming

Raspberry Pi Lazarus application talking to Arduino over serial

FPC application talking to ESP8266 over TCP

FPC application talking to Arduino Nano over TCP