Sunday, 29 July 2012 11:04

Control Arduino via PHP (EasyPHP)

Control Arduino via PHP Control Arduino via PHP

The Arduino Duemilanove (not Arduino Uno) is control by a computer over the network or internet via PHP. This article assumed that you already install WAMP web server & Arduino driver in your computer.

My Arduino Duemilanove is connecting to the USB port of my computer, the USB serial com port is detected as COM4.

  • Microsoft Windows, I'm using Windows XP SP3
  • Web Server, I'm using EasyPHP as my web server.
Determine the COM port
This step is based on Windows XP
  1. Click Start>Settings>Control Panel
  2. Double click System
  3. Click on Hardware tab & click Device Manager button
Your will see your USB serial list under Ports (COM & LPT) as shown in figure below 
COM Port for USB Serial

PHP Source Code: from my website download page
Arduino Sketch: Ocean Control KTA-223


Arduino Source Code
The Arduino source code use in this article is downloaded from Ocean Control. Upload this code to your Arduino if you don't want to buy the device from Ocean Control.


It is very easy to control the Arduino through COM port based on Ocean Control. See example below:

"@00 on 1\r" to turn on LED1
"@00 of 1\r" to turn off LED1
"@00 on 2\r" to turn on LED2
"@00 on 2\r" to turn off LED2

The command string must start will @ & follow by two zero address space which is intended to use for RS485.  But you still can use it for RS232.

Remember to put a \r at the end of the command string, it is use to terminate the string with Carriage Return. 

There is also a very detail PDF file on how to send the command string which can be download from Ocean Control.

PHP Source Code
The PHP code is very simple, but I encounter a problem that the Arduino always cannot be control when the first command send to the Arduino.  I must add a line at line 19 to open the COM port without any valid command in order to make the Arduino to working correctly.


Try & catch is used for error detection. It is also not working in my computer. If the Arduino is not connect to the computer, it just display some nonsense message as below. 

I had make a tutorial to explain how to use the serial command from my Tutorials page:

Running PHP Code
  1. Download the PHP source code & place to the WWW folder of the web server. 
  2. Open Internet Explorer or any browser
  3. Enter http://localhost/phpArduino.php (for EasyPHP 5.39) or http://localhost:8888/phpArduino.php (for EasyPHP 12.0). Note: The port 8888 might be changing.  

You will see 4 buttons display on top left of the browser screen, first two buttons use to turn on & off LED1, the next two buttons use to turn on & off LED2. For example, click on button 1 (label 1 on) will send the string command (@00 on 1\n) to the Arduino to turn on LED1, while click on button 2 (label 1 off) will send the string command (@00 of 1\n) to the Arduino to turn off LED1. 



Read 52184 times Last modified on Friday, 12 September 2014 22:50


  • Comment Link Massimo Friday, 26 October 2018 01:41 posted by Massimo

    Hi, the Link of the Arduino Code is broken, it can be restored ?

    Thank you.

  • Comment Link admin Monday, 18 September 2017 16:44 posted by admin

    Thank You, krishna

  • Comment Link krishna Saturday, 16 September 2017 20:00 posted by krishna

    simple you can control the intensity of light by using

    int led = 9; // the PWM pin the LED is attached to
    int brightness = 0; // how bright the LED is
    int fadeAmount = 5; // how many points to fade the LED by

    // the setup routine runs once when you press reset:
    void setup() {
    // declare pin 9 to be an output:
    pinMode(led, OUTPUT);

    // the loop routine runs over and over again forever:
    void loop() {
    // set the brightness of pin 9:
    analogWrite(led, brightness);

    // change the brightness for next time through the loop:
    brightness = brightness + fadeAmount;

    // reverse the direction of the fading at the ends of the fade:
    if (brightness = 255) {
    fadeAmount = -fadeAmount;
    // wait for 30 milliseconds to see the dimming effect

  • Comment Link Admin Sunday, 09 June 2013 10:29 posted by Admin

    You need to modify Arduino sketch.
    Use analogWrite instead of digitalWrite

    and you need to modify your PHP code to sending value from 0 to 255

  • Comment Link navin Wednesday, 05 June 2013 11:50 posted by navin

    Its works great but i need to control light intensity please give me any idea regarding this

  • Comment Link asf21 Thursday, 25 April 2013 02:37 posted by asf21

    Thanks for your publication.
    I'm modified the sketch to drive two servos for my project but, now, exist a latecy (~3s) between push the button request on php form (led or servodir) and the arduino...
    Any idea?

Leave a comment

Back to Top