Welcome to Blogs @ Andrew Qu
Blog Index
All blogs
Search results

Start Learning Arduino

Summary

I have always wanted to learn MCU (Micro Controller Unit) development, but always feared that it's too hard. Recently, I have heard about Arduino project and did a bit online reading. It appears that with an Arduino board and IDE, things are much easier now. I finally gathered enough courage to start. I will record my experiences as I progress.

Hardware

I bought an Arduino UNO board plus a few other bits (cable, battery wires) for £19.00
Fig. 1

The (blue) cable is a usb cable that connects the board to PC. It supplies power to the board, also, converts the board as a serial (RS232) device attached to PC.

Arduino IDE

Arduino IDE is used to write firmware programs that can run on the Arduino board. It helps you to develop, debug the program. At the end, the program will be downloaded to a MCU in a product that can execute and does tasks intended by the product. For example, to develop a product to monitor room temperature and display on a LED display, you use the Arduino board and IDE to develop and test. Once completed, the program is burnt to a separate MCU and assembled with other hardware component (for example, power supply, thermostat, LED display) to form a product. You do not use the Arduino board in to final product.

Arduino IDE is free and open source. It can be downloaded from https://www.arduino.cc/en/Main/Donate . Installation is simple. I downloaded the Windows Installer for my Windows 10 PC.

Once installed, start Arduino program. Below screen shot is the UI window, not very impressive.

 

First Sketch

Assume, you have started Arduino IDE. In order for the Arduino to connect to the board, we need the port number. To find the port number, first connect the board to your PC using the USB cable. If you have not installed the driver, Windows will prompt you to inatall driver. Sometimes, you need to tell windows the location of the driver software. By default, it will be located at C:\Program Files (x86)\Arduino\drivers.

Next, tell Arduino IDE which port to connect. To do this, click the menu item "Tools", then "Port:" and select the "Arduino Uno" port (normally COM3).

In Arduino, a program is called a "Sketch". The IDE includes a few built-in sketches. The first example I tried is LED blinking. This example, blinks the built-in LED light on the Arduino board connected to pin 13. To select this example, click menu item

File -> Examples -> 01.Basic -> Blink

It will open another window and display the code.

In the new window, click button 1 (compile) and then button 2 (upload), as shown in the screenshot above (button location; top/left corner). Now observe the LED on the Arduino board, it will be on for 1s and then off for 1s.

Change the delay(ms) value for ms to see different effect. The time delay is in milli-second.

Serial Communication

Serial (RS232) communication is important. This is the way your device is going to talk to other devices, if needed. In this example, your PC is the other device. We will let the PC talk to the Arduino board. Of course, by uploading the program onto the board, the PC is already talking to the Arduino board. But that's more complex.

The sketch

int val;
int ledpin=13;

void setup() {
  Serial.begin(9600);
  pinMode(ledpin, OUTPUT);
}

void loop() {
  val=Serial.read();
  if(val=='R')
  {
    digitalWrite(ledpin,HIGH);
    delay(500);
    digitalWrite(ledpin,LOW);
    delay(500);
    Serial.println("Hello world!");
  }
}

What does it do ?

When this program is run inside the MCU, it will keep reading the serial port (connected to the PC through the USB cable). When we send a letter R from PC to the board, the LED will light for 500 ms, then off. It then sends a line "Hello world!" back to the PC.

How can the PC talk to the board ? A serial port monitoring program is needed, for example, RealTerm, SerialMon. Fortunately, Arduino IDE has a built-in serial monitor. Just click the icon on the top right corner will open the serial monitor, see Fig.1

Operations

Copy the code into Adruino IDE. As usual, click button 1 to compile and 2 to upload. Once uploaded, the program starts running immediately inside the MCU, waiting for input on the serial port from PC.

Now open the Serial Monitor and enter R, then click button [Send], as shown in the screenshot above. You will notice the LED will light, then off, then you get a line back in the Serial Monitor.

Try type RRRR and then [send], you'll notice the LED light will blink 4 times and 4 lines printd in the serial monitor. If you watch closely, you will notice that the LED light marked RX will light once, followed by LED light and TX light 4 times. TX light once indicates the MCU read "RRR" in one go. Then our program reads one character from the MCU's serial buffer (Serial.read()), then light the LED (digitalWrite(ledpin, HIGH)), then turn it off with parameter LOW. Our program then writes the line "Hello world!". The MCU transmits this line back to PC (TX light blinks).

This demonstrates that the board is talking to the PC. Thiss is independent of the Arduino IDE. Other serial monitor program will also work.

LED Blink Mounted on a Bread Board

In this exercise, instead of blinking the built-in LED connected to PIN 13, I am going to connect an LED to PIN 10 and make it blink. Hardware components required:

  • 1 x Bread board
  • 1 x LED
  • 1 x 220Ω Resistor
  • 2 x jump wires

The Wiring

  • Unplug the board from the PC before making or changing any connection.
  • LED is directional, the longer leg is positive.
  • The 5 holes on the vertical line (marked yellow) is internally coonnected.
  • A resistor is not directional. Therefore, it can be connected in either way.

The sketch

int i;
int ledpin=10;

void setup() {
  pinMode(ledpin, OUTPUT);
}

void loop() {
    for(i=0; i<2; i++)
    {
      digitalWrite(ledpin,HIGH);
      delay(250);
      digitalWrite(ledpin,LOW);
      delay(250);
    }
    delay(2500);
}
END
Next



Ads from Googlee
Dr Li Anchor Profi
www.anchorprofi.de
Engineering anchorage plate design system
©Andrew Qu, 2014. All rights reserved. Code snippets may be used "AS IS" without any kind of warranty. DIY tips may be followed at your own risk.