Manufacturers

RSS feed

No RSS feed added

How to compile and run Arduino sketches on the pcDuino

The quickest way to get going with Arduino sketches is to start with the samples so you have a ‘template’ to guide you.
For parts of this guide to work, you’re pcDuino needs to be connected to the internet and you also need a terminal session running on the pcDuino.

You are typing red text. What you should expect to see is green text. The prompt will look something like the blue text.

Setup (one time)


If not already done, set up git. Do this using the command:
ubuntu@ubuntu:~$ sudo apt-get install git

Make sure you’re in your home folder by typing
ubuntu@ubuntu:~$ cd
ubuntu@ubuntu:~$ pwd
/home/Ubuntu

Now download the distribution from github by typing
ubuntu@ubuntu:~$ git clone https://github.com/pcduino/c_enviroment
Cloning into 'c_enviroment'...
remote: Counting objects: 250, done.
remote: Compressing objects: 100% (166/166), done.
remote: Total 250 (delta 87), reused 232 (delta 69)
Receiving objects: 100% (250/250), 302.59 KiB | 78 KiB/s, done.
Resolving deltas: 100% (87/87), done.

You should now have a folder called c_enviroment. You can check by typing ls
ubuntu@ubuntu:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos arduino c_enviroment sample

Initial look around

Change into the c_envoroment folder
ubuntu@ubuntu:~$ cd c_enviroment
ubuntu@ubuntu:~/c_enviroment$ ls
Makefile hardware libraries output sample

Now run make to make the libraries and the examples with the following command
ubuntu@ubuntu:~/c_enviroment$ make

You should see a series of compile commands occurring without errors with the last line being:
make[1]: Leaving directory `/home/ubuntu/c_enviroment/sample'

The resulting binary files are found in the output/test folder
ubuntu@ubuntu:~/c_enviroment$ cd output/test
ubuntu@ubuntu:~/c_enviroment/output/test$ ll
total 660
drwxrwxr-x 2 ubuntu ubuntu 4096 Apr 27 06:59 ./
drwxrwxr-x 3 ubuntu ubuntu 4096 Apr 27 06:49 ../
-rwxrwxr-x 1 ubuntu ubuntu 13868 Apr 27 06:58 adc_test*
-rwxrwxr-x 1 ubuntu ubuntu 28284 Apr 27 06:58 adxl345_test*
-rwxrwxr-x 1 ubuntu ubuntu 14209 Apr 27 06:58 interrupt_test*
-rwxrwxr-x 1 ubuntu ubuntu 13726 Apr 27 06:58 io_test*
-rwxrwxr-x 1 ubuntu ubuntu 13712 Apr 27 06:59 linker_button_test*
-rwxrwxr-x 1 ubuntu ubuntu 13907 Apr 27 06:59 linker_buzzer_test*
-rwxrwxr-x 1 ubuntu ubuntu 13689 Apr 27 06:59 linker_hall_sensor_test*
-rwxrwxr-x 1 ubuntu ubuntu 13760 Apr 27 06:59 linker_joystick_test*
-rwxrwxr-x 1 ubuntu ubuntu 13769 Apr 27 06:59 linker_led_bar_test*
-rwxrwxr-x 1 ubuntu ubuntu 13690 Apr 27 06:59 linker_led_test*
-rwxrwxr-x 1 ubuntu ubuntu 14290 Apr 27 06:59 linker_light_sensor_test*
-rwxrwxr-x 1 ubuntu ubuntu 13696 Apr 27 06:59 linker_magnetic_sensor_test*
-rwxrwxr-x 1 ubuntu ubuntu 14175 Apr 27 06:59 linker_potentiometer_test*
-rwxrwxr-x 1 ubuntu ubuntu 13658 Apr 27 06:59 linker_relay_test*
-rwxrwxr-x 1 ubuntu ubuntu 28530 Apr 27 06:59 linker_rtc_test*
-rwxrwxr-x 1 ubuntu ubuntu 14136 Apr 27 06:59 linker_sound_sensor_test*
-rwxrwxr-x 1 ubuntu ubuntu 13832 Apr 27 06:59 linker_temperature_sensor_test*
-rwxrwxr-x 1 ubuntu ubuntu 13714 Apr 27 06:59 linker_tilt_test*
-rwxrwxr-x 1 ubuntu ubuntu 13689 Apr 27 06:59 linker_touch_sensor_test*
-rwxrwxr-x 1 ubuntu ubuntu 40523 Apr 27 06:58 liquidcrystal_i2c*
-rwxrwxr-x 1 ubuntu ubuntu 40523 Apr 27 06:58 liquidcrystal_spi*
-rwxrwxr-x 1 ubuntu ubuntu 38242 Apr 27 06:59 pn532_readAllMemoryBlocks*
-rwxrwxr-x 1 ubuntu ubuntu 38238 Apr 27 06:59 pn532readMifareMemory*
-rwxrwxr-x 1 ubuntu ubuntu 38240 Apr 27 06:59 pn532readMifareTargetID*
-rwxrwxr-x 1 ubuntu ubuntu 38263 Apr 27 06:59 pn532writeMifareMemory*
-rwxrwxr-x 1 ubuntu ubuntu 13842 Apr 27 06:58 pwm_test*
-rwxrwxr-x 1 ubuntu ubuntu 32336 Apr 27 06:58 serial_test*
-rwxrwxr-x 1 ubuntu ubuntu 14449 Apr 27 06:58 spi_test*
-rwxrwxr-x 1 ubuntu ubuntu 18822 Apr 27 06:59 tone_test*

The source for each one is in sample folder
ubuntu@ubuntu:~/c_enviroment/output/test$ cd ../../sample
ubuntu@ubuntu:~/c_enviroment/sample$ ll
total 148
drwxrwxr-x 2 ubuntu ubuntu 4096 Apr 27 06:49 ./
drwxr-xr-x 7 ubuntu ubuntu 4096 Apr 27 06:58 ../
-rw-rw-r-- 1 ubuntu ubuntu 1368 Apr 27 06:49 Makefile
-rw-rw-r-- 1 ubuntu ubuntu 467 Apr 27 06:49 adc_test.c
-rw-rw-r-- 1 ubuntu ubuntu 6589 Apr 27 06:49 adx.c
-rw-rw-r-- 1 ubuntu ubuntu 5416 Apr 27 06:49 adxl345_test.c
-rw-rw-r-- 1 ubuntu ubuntu 92 Apr 27 06:49 core.h
-rw-rw-r-- 1 ubuntu ubuntu 830 Apr 27 06:49 interrupt_test.c
-rw-rw-r-- 1 ubuntu ubuntu 781 Apr 27 06:49 io_test.c
-rw-rw-r-- 1 ubuntu ubuntu 430 Apr 27 06:49 linker_button_test.c
-rw-rw-r-- 1 ubuntu ubuntu 1420 Apr 27 06:49 linker_buzzer_test.c
-rw-rw-r-- 1 ubuntu ubuntu 390 Apr 27 06:49 linker_hall_sensor_test.c
-rw-rw-r-- 1 ubuntu ubuntu 327 Apr 27 06:49 linker_joystick_test.c
-rw-rw-r-- 1 ubuntu ubuntu 2202 Apr 27 06:49 linker_led_bar_test.c
-rw-rw-r-- 1 ubuntu ubuntu 613 Apr 27 06:49 linker_led_test.c
-rw-rw-r-- 1 ubuntu ubuntu 1197 Apr 27 06:49 linker_light_sensor_test.c
-rw-rw-r-- 1 ubuntu ubuntu 404 Apr 27 06:49 linker_magnetic_sensor_test.c
-rw-rw-r-- 1 ubuntu ubuntu 647 Apr 27 06:49 linker_potentiometer_test.c
-rw-rw-r-- 1 ubuntu ubuntu 331 Apr 27 06:49 linker_relay_test.c
-rw-rw-r-- 1 ubuntu ubuntu 2505 Apr 27 06:49 linker_rtc_test.c
-rw-rw-r-- 1 ubuntu ubuntu 646 Apr 27 06:49 linker_sound_sensor_test.c
-rw-rw-r-- 1 ubuntu ubuntu 1145 Apr 27 06:49 linker_temperature_sensor_test.c
-rw-rw-r-- 1 ubuntu ubuntu 405 Apr 27 06:49 linker_tilt_test.c
-rw-rw-r-- 1 ubuntu ubuntu 389 Apr 27 06:49 linker_touch_sensor_test.c
-rw-rw-r-- 1 ubuntu ubuntu 797 Apr 27 06:49 liquidcrystal_i2c.c
-rw-rw-r-- 1 ubuntu ubuntu 831 Apr 27 06:49 liquidcrystal_spi.c
-rw-rw-r-- 1 ubuntu ubuntu 2002 Apr 27 06:49 pitches.h
-rw-rw-r-- 1 ubuntu ubuntu 3149 Apr 27 06:49 pn532_readAllMemoryBlocks.c
-rw-rw-r-- 1 ubuntu ubuntu 1690 Apr 27 06:49 pn532readMifareMemory.c
-rw-rw-r-- 1 ubuntu ubuntu 1117 Apr 27 06:49 pn532readMifareTargetID.c
-rw-rw-r-- 1 ubuntu ubuntu 2326 Apr 27 06:49 pn532writeMifareMemory.c
-rw-rw-r-- 1 ubuntu ubuntu 498 Apr 27 06:49 pwm_test.c
-rw-rw-r-- 1 ubuntu ubuntu 2079 Apr 27 06:49 serial_test.c
-rw-rw-r-- 1 ubuntu ubuntu 785 Apr 27 06:49 spi_test.c

To view the contents of a sample sketch, (this example we’ll look at the contents of linker_led_test.c) type:
ubuntu@ubuntu:~/c_enviroment/sample$ cat linker_led_test.c
/*
* LED test program
*/
#include <core.h>
int led_pin = 1;

void setup()
{
if(argc != 2){
goto _help;
}
led_pin = atoi(argv[1]);
if((led_pin < 0) || (led_pin > 13)){
goto _help;
}
pinMode(led_pin, OUTPUT);
return;

_help:
printf("Usage %s LED_PIN_NUM(0-13)n", argv[0]);
exit(-1);
}
void loop()
{
digitalWrite(led_pin, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(led_pin, LOW); // set the LED off
delay(1000); // wait for a second
}

Editing an existing sketch

You will probably want to change some of these samples as an initial play so will need a text editor. We tend to use nano, but you may already have a favourite. If not, install nano by typing:
ubuntu@ubuntu:~/c_enviroment/sample$ sudo apt-get install nano

You should see nano being downloaded and installed.

Let’s use the same sketch as the example, type:
ubuntu@ubuntu:~/c_enviroment/sample$ nano linker_led_test.c
You should see something like:

nano on pcDuino with basic Arduino script

A simple tutorial on how to use nano is: http://www.howtogeek.com/howto/42980/the-beginners-guide-to-nano-the-linux-command-line-text-editor/

One thing you may notice with this sketch, it compiles to the command line. This example shows how you can pass parameters to the sketch when calling it from the command line. This is optional as you will see in some of the other sketches, but does add some benefit to writing sketches to run on the pcDuino.

Creating your own sketch

Now lets create our own sketch and work out how to compile it so it runs. It will be a button on pin 7 that when pressed, turns on an LED on pin 8.

While in the sample folder, type:
ubuntu@ubuntu:~/c_enviroment/sample$ nano button_led.c

An empty nano screen should appear.

Copy and paste the following code into it. (Remember to paste in nano at the cursor, just right click the mouse button).

#include <core.h> // Required first line to run on pcDuino
int ledPin = 8;
int buttonPin = 7;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

Above sketch modified from http://arduino.cc/en/tutorial/button

Modify the Makefile and compile

Now we need to add this new sketch to the Makefile in the samples folder. Open the Makefile with nano (or your favourite text editor)
ubuntu@ubuntu:~/c_enviroment/sample$ nano Makefile

You will see a section that lists all the OBJS something like:
OBJS = io_test adc_test pwm_test spi_test adxl345_test serial_test liquidcrystal_i2c liquidcrystal_spi interrupt_test tone_test
OBJS += linker_led_test linker_potentiometer_test linker_tilt_test linker_light_sensor_test linker_button_test
OBJS += linker_touch_sensor_test linker_magnetic_sensor_test linker_temperature_sensor_test linker_joystick_test
OBJS += linker_rtc_test linker_sound_sensor_test linker_buzzer_test linker_hall_sensor_test linker_led_bar_test linker_relay_test
OBJS += pn532_readAllMemoryBlocks pn532readMifareMemory pn532readMifareTargetID pn532writeMifareMemory

We’re going to add a line to the end of this with the name of the scketch we just created:
OBJS += button_led

Note, we don’t put the .c on the end.

Save the file and exit nano using <CTRL>X with a y and <enter>

We now run make by typing
ubuntu@ubuntu:~/c_enviroment/sample$ make

You should see a whole bunch of text with the end being:
button_led.c -o ../output/test/button_led ../libarduino.a

If all went well, you can go to the output/test folder and find your executable you have created:
ubuntu@ubuntu:~/c_enviroment/sample$ cd ../output/test/
ubuntu@ubuntu:~/c_enviroment/output/test$ ll
total 676
drwxrwxr-x 2 ubuntu ubuntu 4096 Apr 27 07:51 ./
drwxrwxr-x 3 ubuntu ubuntu 4096 Apr 27 06:49 ../
-rwxrwxr-x 1 ubuntu ubuntu 13868 Apr 27 07:51 adc_test*
-rwxrwxr-x 1 ubuntu ubuntu 28284 Apr 27 07:51 adxl345_test*
-rwxrwxr-x 1 ubuntu ubuntu 13668 Apr 27 07:51 button_led*
…..(not showing rest of listing here)

Run your sketch

To run it, once you have wired up a switch and led to the right pins, type:
ubuntu@ubuntu:~/c_enviroment/output/test$ ./button_led

To stop the program, <Ctrl>C

A quick re-cap:

  • Add #include <core.h> to the top of your sketch.
  • Create your sketch in the samples folder (if your familiar with linux, makefiles, and compiling code, you could set up your own)
  • Add the filename to the Makefile in the samples folder in the OBJS section without the .c
  • Run make
  • Run the executable from the output/test folder.
  • You can introduce command line arguments into your sketch to make it more transportable.