Sunday, February 7, 2016

Tutorial: Arduino Programming

Contents:

  • Setting up Arduino Environment
  • Flashing LED Circuit
  • Test1 - External LED
  • Test2 - Internal LED
  • Temperature/Humidity Sensor Circuit
  • ARRAYS - Test with more LEDs
  • BUTTON Test
  • Difference setting up and programming interface using two different source
  •  References

Setting up Arduino Environment:


To interact with Arduino board from our system we need to setup required software installed in the system. Follow below steps to install Arduino IDE,

  • Download and install arduino IDE from the link: https://www.arduino.cc/en/Main/Software
  • After installation, add the ESP8266 package by following these steps:
  • In the Arduino IDE, open the Preferences window.
  • Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json in the Addiotional Board Manager URLs text field.
  • Open Board Manager from Tools and install esp8266 platform.
  • Setup ESP8266 support: Select Tools --> Board --> NodeMCU 1.0(ESP-12E Module)
  • CPU Frequency: Select Tools --> CPU Frequency --> 80 MHz
  • Upload Speed: Select Tools --> Upload Speed --> 115200
  • Finally selecting a port: Select one of the ports available in the list: Tools --> Port

Following diagram represents the ESP8266MOD and its configuration information, it helps to make connections easier (pins, GPIOs etc.)


1. Flashing LED Circuit:

Arduino ESP8266MOD provides easy environment to do experiments with minimum number of connections.

Test1 - External LED:

The following code works for GPIO#7 (pin#13 or D7),

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(750);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(750);              // wait for a second
}

Output: Now, you can see that the external LED blinks, with delay as 750 milliseconds.


Test2 - Internal LED:

The following code works for pin#16,

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(16, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(16, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);              // wait for a second
  digitalWrite(16, LOW);    // turn the LED off by making the voltage LOW
  delay(500);              // wait for a second
}

Output: Now, you can see that the internal LED blinks, with delay as 500 milliseconds.


2. Temperature/Humidity Sensor Circuit:


The DHT sensor is much easier to connect:
  • Plug the pin number 1 to the Arduino’s 3.3V pin
  • Pin number 4 to GND
  • Pin number 2 to Arduino pin 13(D7 or GPIO#7)
  • Finally, put the 10K resistor between the sensor pins number 1 and 2


Code:

// Include required libraries
                       #include <SPI.h>
                       #include <string.h>
                       #include "DHT.h"

                       // DHT11 sensor pins
                       #define DHTPIN 13
                       #define DHTTYPE DHT11

                       // DHT instance
                       DHT dht(DHTPIN, DHTTYPE);
                                     
                        void setup(void)
                       {

                        // Initialize DHT sensor
                        dht.begin();

                        Serial.begin(115200);

                        }

                        void loop(void)
                        {

                        // Measure the humidity & temperature
                        float h = dht.readHumidity();
                        float t = dht.readTemperature();

                        // Transform to String
                        String temp = String((int) t);
                        String hum = String((int) h);

                        Serial.print("Temperature: ");
                        Serial.println(temp);
                        Serial.print("Humidity: ");
                        Serial.println(hum);
                        Serial.println("");
                        delay(1000);
                        }

The following code interacts with Arduino board and read the responses of DHT11 and displays the output. To see the result, Select Tools --> Serial Monitor



Final report is shown below,



ARRAYS - Test with more LEDs:

A serial of LEDs connected, and tested for blink one after another


Following code enables the serial(array) of LEDs to turn on:

int timer = 200;           // The higher the number, the slower the timing.
int ledPins[] = {
  5, 4, 2, 14, 12, 15
};       // an array of pin numbers to which LEDs are attached
int pinCount = 6;           // the number of pins (i.e. the length of the array)

void setup() {
  // the array elements are numbered from 0 to (pinCount - 1).
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT);
  }
}

void loop() {
  // loop from the lowest pin to the highest:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);

  }

  // loop from the highest pin to the lowest:
  for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);
  }
}


BUTTON Test:

A button connected to ESP8266MOD as follows,

  • One pin of button is connected to the GND(ground)
  • Another end of button is connected to the one end of 220 Ohms resistor
  • Other end of resistor is connected to 3.3V pin
  • Finally, LED is connected as anode to GPIO pin annd cathode to GND




Code:

// set pin numbers:
const int buttonPin = 14;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// 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) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}

Difference setting up and programming interface using two different source ( laptop and pi ):

I wanted to try to get Raspberry talking to an Arduino over USB using Python.

There is a Python library for serial communications called 'pySerial' which has history with Arduino. You can find more information here: http://playground.arduino.cc/Interfacing/Python


  • First, install Arduino: $ sudo apt-get install arduino
  • Next, install the nanpy source:  This is needed to later build the new Arduino firmware:
1 $ cd ~
2 $ curl -O https://pypi.python.org/packages/source/n/nanpy/nanpy-v0.8.tar.gz
3 $ tar xvf nanpy-v0.8.tar.gz
4 $ rm nanpy-v0.8.tar.gz
  • Now install the required Python libs:
1 $ sudo pip install nanpy
2 $ sudo pip install pyserial
  • Hook up your Arduino to one of the Pi’s USB ports, and create/upload the new firmware (using an Arduino Uno as an example):
1 $ cd ~/nanpy/firmware
2 $ export BOARD=uno
3 $ make
4 $ make upload
NOTE: When I am trying to run make upload, the command throws an error. Later I understand that the downloaded arduino IDE is of 1.0.x version instead 1.6.x version. In this old version of arduino IDE, there is no setup for ESP8266MOD. When I am trying to install ESP8266 plugin there is no option to go to Board Manager in Tools. Hence I could not able to do test on raspeberry pi - arduino integration through with old version. I will try to install newer version and make necessary installation and setup, as well as the basic tests of raspberry pi - arduino integration(data exchange).

References:
  • http://www.akeric.com/blog/?p=2420
  • http://www.doctormonk.com/2012/04/raspberry-pi-and-arduino.html
  • http://playground.arduino.cc/Interfacing/Python
  • http://www.instructables.com/id/The-Raspberry-Pi-Arduino-Connection/
  • http://blog.oscarliang.net/connect-raspberry-pi-and-arduino-usb-cable/
  • http://www.seeedstudio.com/recipe/166-basic-pi-lt-gt-arduino-communication-over-usb.html
  • http://playground.arduino.cc/Linux/Ubuntu
  • http://blogspot.tenettech.com/?p=2723
  • http://razzpisampler.oreilly.com/ch10.html
  • http://spellfoundry.com/sleepy-pi/setting-arduino-ide-raspbian/


No comments:

Post a Comment