Sign in Join
Wiki copytemporary

Arduino: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11*(Arduino only)*
22
33Many [Arduino](https://en.wikipedia.org/wiki/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.
44
55For Lazarus developers Arduino opens a whole new world of smart physical sensors and actuators for monitoring, control and data acquisition.
66
77The fpc compiler is even capable to compile programs for Arduino boards itself.
88
9-<figure>
10-<img src="Arduino_Uno_R3.png" title="Arduino_Uno_R3.png" />
11-<figcaption>Arduino_Uno_R3.png</figcaption>
12-</figure>
9+![](/wiki-images/6/6d/Arduino_Uno_R3.png)
1310
1411# FPC on Arduino
1512
1613Currently (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.
1714
1815There are further wiki pages for programing AVR microcontrollers: [[AVR]], [[AVR Programming]]
1916
2017There is also a tutorial on how to use Lazarus on a AtMega328p (Arduino Uno/Nano) in German language: [[AVR Embedded Tutorial/de]]
2118
2219# Communication between a Lazarus application and Arduino
2320
2421The 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.
2522
2623## One-way serial communication example using Synaser
2724
2825Here we show how to communicate with your board through serial communication channel.
2926
3027### Lazarus side serial application
3128
3229There are many ways for [[Hardware Access|serial]] communication in Lazarus and FPC.
3330
3431#### Minimal Lazarus Synaser serial example
3532
3633We will use [Synaser](http://synapse.ararat.cz/doc/help/synaser.html) from [Synapse library](http://www.ararat.cz/synapse/doku.php) 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:
3734
3835``` pascal
3936procedure TForm1.Button1Click(Sender: TObject);
4037var
4138 ser: TBlockSerial;
4239begin
4340 ser := TBlockSerial.Create;
4441 try
4542 ser.Connect('COM1'); // write here Arduino COM port number (on linux it's something like '/dev/ttyUSB0')
4643 Sleep(250);
4744 ser.config(9600, 8, 'N', SB1, False, False);
4845 ser.SendString('on'); // button 2 should have 'off' here
4946 finally
5047 ser.free;
5148 end;
5249end;
5350```
5451
5552### Arduino side serial application
5653
5754You can program your Arduino in several languages.
5855
5956#### Minimal Arduino C sketch serial example
6057
6158You can follow [this](http://www.chrisbrand.co.za/2013/06/30/arduino-serial-port/) 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](https://www.arduino.cc/en/Main/Software), and download it to your board:
6259
6360``` cpp
6461int led = 13; // Pin 13
6562
6663void setup()
6764{
6865 pinMode(led, OUTPUT); // Set pin 13 as digital out
6966
7067 // Start up serial connection
7168 Serial.begin(9600); // baud rate
7269 Serial.flush();
7370}
7471
7572void loop()
7673{
7774 String input = "";
7875
7976 // Read any serial input
8077 while (Serial.available() > 0)
8178 {
8279 input += (char) Serial.read(); // Read in one char at a time
8380 delay(5); // Delay for 5 ms so the next char has time to be received
8481
8582 if (input == "on")
8683 {
8784 digitalWrite(led, HIGH); // on
8885 }
8986 else if (input == "off")
9087 {
9188 digitalWrite(led, LOW); // off
9289 }
9390 }
9491}
9592```
9693
9794#### Minimal Arduino mikroPascal serial example
9895
9996[mikroPascal for AVR](https://www.mikroe.com/mikropascal/) can be used to develop Arduino programs. TBD.
10097
10198#### Minimal Arduino E-Lab AvrCo serial example
10299
103100[E-Lab AvrCo Multitasking Pascal](http://e-lab.de/AVRco/index_en.html) can be used to develop Arduino programs. TBD.
104101
105102#### Minimal Arduino FPC serial example
106103
107104Yes, your Arduino 8-bit AVR board [[AVR Programming|can be programmed with FPC]] too. TBD.
108105
109106## Bidirectional serial communication example using SDPO
110107
111108The single directional example is based on synaser, which is not a visual component. SDPO provides a visual wrapper around Synaser, and some people may find this method easier. The following example requires the SDPO package (currently 0.4.0) to be installed in Lazarus. There are other tutorials to show you how to install package. If you cannot find the SDPO component on your component palette, this example will not work.
112109
113110#### Lazarus Side of Bidirectional SDPO serial example
114111
115112Open a new project, and add the following to a form: (Names are the default names)
116113
1171141. the SDPO port (under the SDPO tab after you have installed it).
1181152. four buttons. Caption as follows: Button1 SEND; Button2 CLEAR; Button3 UP; Button4 DOWN;
1191163. a memo. Used to display what is sent and received.
1201174. a panel. Used to display which number is being sent.
121118
122119Configure the Sdposerial1 properties to match the Arduino with the object inspector.
123120
124121- Baudrate = br__9600;
125122- Device = YourSerialPort ( mine is /dev/ttyUSB0 ). This is probably the same port you use to program your Arduino.
126123- FlowControl = fcNone;
127124- Parity = pNone;
128125- StopBits = sbOne;
129126- SynSer = (TBlockSerial); (notice this from the previous example?)
130127- Tag = 0;
131128
132129Using the code window, find the spot under var where it defines **Form1: Tform;** , and add the next three variables:
133130
134131``` pascal
135132var
136133 Form1: TForm1;
137134 incomingstring: String;
138135 completestring: String;
139136 messagenum: integer;
140137```
141138
142139Add the following events (code is shown below)
143140
144141- OnCreate for Form1
145142- OnDestroy for Form1
146143- OnRxData for Sdposerial1
147144- OnClick for each button (4 events)
148145
149146In the empty code blocks for each event, add the following code.
150147
151148``` pascal
152149procedure TForm1.SdpoSerial1RxData(Sender: TObject);
153150begin
154151 // add whatever is in the serial buffer to the incoming string
155152 incomingstring := incomingstring + SdpoSerial1.ReadData;
156153 // arduino terminates a serial.println transmission with a CR (#13) and a LF (#10).
157154 // check to see if our incming string is terminated by a linefeed
158155 if (incomingstring[Length(incomingstring)] = #10 ) then
159156 begin
160157 // the string ends a linefeed, so the whole string from the arduino is here.
161158 // remove the extra two characters before adding the incoming string to the memo (adds extra lines if you dont)
162159 completestring := LeftStr(incomingstring,Length(incomingstring)-2); // trim two places
163160 Memo1.Lines.add(completestring); // echo the new string to the memo
164161 incomingstring := ''; // clear the incoming string so we can start again
165162 end;
166163end;
167164
168165procedure TForm1.FormCreate(Sender: TObject);
169166begin
170167 incomingstring := '';
171168 SdpoSerial1.open;
172169 SdpoSerial1.Active := true;
173170end;
174171
175172procedure TForm1.Button3Click(Sender: TObject);
176173// request to go higher
177174begin
178175 // check to see if we have reached the upper limit
179176 if (messagenum < 12) then
180177 begin
181178 messagenum := messagenum + 1; // increase the number
182179 panel1.caption := inttostr(messagenum); // display the new number
183180 end;
184181 // if we have reached the limit, this won't do anything
185182end;
186183
187184procedure TForm1.Button2Click(Sender: TObject);
188185begin
189186 memo1.lines.clear; // clear the memo
190187end;
191188
192189procedure TForm1.Button1Click(Sender: TObject);
193190// send a request to the arduino
194191begin
195192 memo1.lines.add(panel1.caption); // echo the request to the memo
196193 SdpoSerial1.WriteData(panel1.caption + #13#10);
197194end;
198195
199196procedure TForm1.Button4Click(Sender: TObject);
200197// request to go lower
201198begin
202199 // check to see if we have reached the lower limit
203200 if (messagenum > 0) then
204201 begin
205202 messagenum := messagenum - 1; // decrease the number
206203 panel1.caption := inttostr(messagenum); // display the new number
207204 end;
208205 // if we have reached the limit, this won't do anything
209206end;
210207
211208procedure TForm1.FormDestroy(Sender: TObject);
212209begin
213210 SdpoSerial1.Active := false;
214211 SdpoSerial1.close;
215212end;
216213
217214```
218215
219216Test the program, and verify that it compiles.
220217
221218### Arduino side of Bidirectional serial example application
222219
223220The following code has been tested on a Arduino Mega 2560 clone board. Feel free to alter it as you see fit. It is provided as one block to avoid adding your own strings.
224221
225222``` cpp
226223//** This program is to demo simple bidirectional serial communication
227224//** between an Arduino a computer running Lazarus. This is one side
228225//** of the tutorial. See https://wiki.freepascal.org/Arduino for more
229226//** details. Tested on Linux Mint with off-brand Arduino mega 2560.
230227//** Program draws heavily on the work of others. Use program freely,
231228//** but at your own risk.
232229
233230char incoming;
234231String textnum;
235232int number;
236233String txt[12];
237234
238235void setup()
239236{
240237 // Start up serial connection
241238 Serial.begin(9600); // baud rate
242239 Serial.flush();
243240 delay(1000); // give the port time to work
244241 txt[ 0] = "You asked for message 0.";
245242 txt[ 1] = "You asked for message 1.";
246243 txt[ 2] = "You asked for message 2.";
247244 txt[ 3] = "You asked for message 3.";
248245 txt[ 4] = "You asked for message 4.";
249246 txt[ 5] = "You asked for message 5.";
250247 txt[ 6] = "You asked for message 6.";
251248 txt[ 7] = "You asked for message 7.";
252249 txt[ 8] = "You asked for message 8.";
253250 txt[ 9] = "You asked for message 9.";
254251 txt[10] = "You asked for message 10.";
255252 txt[11] = "You asked for message 11.";
256253 txt[12] = "ERROR - SOMETHING WENT WRONG";
257254}
258255
259256void loop()
260257{
261258 textnum = ""; // clear the number
262259 number = 12; // set the number to give the error message
263260
264261 // Read any serial input
265262 while (Serial.available() > 0)
266263 {
267264 incoming = (char) Serial.read(); // Read in one char
268265 if (isdigit(incoming)) textnum += incoming;
269266 delay(50); // Delay for 50 ms so the next char has time to be received
270267 }
271268
272269 if (textnum != "")
273270 {
274271 number = String(textnum).toInt(); // convert the incoming string to a number
275272 Serial.println(txt[number]); // send the appropriate string back
276273 }
277274
278275
279276}
280277```
281278
282279Compile and upload this code to your board. You should be able to test the program by opening the Serial Monitor in Arduino, and sending a number from 0 to 32 to the Arduino. It should respond by "insulting" you with the appropriate numbered insult. No error checking is provided, so keep your requests in range.
283280
284281### Connecting the Two Sides
285282
286283Serial Ports cannot be opened by more than one program at a time - particularly when a program grabs the port and hogs it like this simple example does. Make sure you close the Arduino Serial Monitor before attempting to run the Lazarus program. Your Arduino should be plugged in and powered up. If you are working under Linux, you may need to run the program as a superuser, or you may need to add your user to the dialout group. In any case, once the Arduino is up and running, you can run the program. It must be able to connect with the Arduino when it creates the main window, and it will hog the port until you properly close the application. Depending on your computer, sleeping or hibernating may mess up your communications.
287284
288285## Ethernet communication
289286
290287Here we show how to communicate with your board through ethernet communication channel.
291288
292289### Lazarus side ethernet application
293290
294291TBD
295292
296293### Arduino side ethernet application
297294
298295TBD
299296
300297## Wiki Resources
301298
302299- [[Portal:Embedded|Embedded Portal]]
303300- [[AVR]]
304301- [[AVR Programming]]
305302
306303## External Resources
307304
308305- [Forum topic that started this article](http://forum.lazarus.freepascal.org/index.php?topic=36885)
309306- [Raspberry Pi Lazarus application talking to Arduino over serial](https://bigdanzblog.wordpress.com/2014/11/07/serial-communications-usb-between-a-raspberry-pi-and-an-arduino-using-lazarus-free-pascal/)
310307- [FPC application talking to ESP8266 over TCP](https://bigdanzblog.wordpress.com/2016/02/14/using-lazarus-free-pascal-to-communicate-with-esp8266/)
311308- [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/)
312309- [Arduino projects](https://www.allaboutcircuits.com/projects/category/arduino/)
313310
314311---
315312*From the Free Pascal wiki, page "Arduino", as it stood on 2023-08-22 after a change by AllPascal, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history