Arduino control over serial

I hope that you are familiar with Arduino basics. This post is just to show you how to control Arduino over serial data sent through keyboard.

Open your Arduino IDE.

Start by allocating a variable name to our LED and setting it as output.

const int ledPin = 13;
void setup()
{
  pinMode(ledPin, OUTPUT);
}
void loop()
{
}

To communicate with our processing sketch you need to initialize the serial communication; this can be done by calling the ‘begin()’ method on the ‘Serial’ object. The syntax used for initializing the Serial port is:

Serial.begin(baud_rate);

Baud is a variable unit of data transmission speed, or simply 1 baud = 1 symbol per second. A baud rate of 9600 will be fine for us.

Serial.begin(9600);

Our serial port is initialized now need to check for any available data. Therefore, in our loop() function we use a simple if statement to check if there is any available data.

if(Serial.available())
{
}

If Serial.available() evaluates to true, then ‘if’ block will be executed.

We have opened up the serial port & checked for data availability over serial, now our next job is to actually use the data to command Arduino to do desired. Declare a variable of type char called inputValue and assign it to the data available in the serial buffer.

char inputValue = Serial.read();

The read() method takes the data available in the Serial buffer and stores it to inputValue variable. This statement is typed inside the ‘if’ block.

Now, if the data we receive is ‘H’ we will turn on the LED if it is ‘L’ we will turn off the LED, if it is neither ‘H’ or ‘L’ do nothing.

if(inputValue == ‘H’)
{
  digitalWrite(ledPin, HIGH);
}
else if(inputValue == ‘L’)
{
  digitalWrite(ledPin, HIGH);
}
else
{
  //  do nothing if anything other than a ‘H’ or a ‘L’
}

At the end of the loop() function add a small delay() of 10 milliseconds to avoid overloading the Arduino with data.

Source Code

const int ledPin = 13;
void setup()
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}
void loop()
{
  if (Serial.available())
  {
    char inputValue = Serial.read();
    if(inputValue == 'H')
    {
      digitalWrite(ledPin, HIGH);
    }
    else if(inputValue == 'L')
    {
      digitalWrite(ledPin, LOW);
    }
    else
    {
      // we will do nothing if the data is anything other than a ‘H’ or a ‘L’
    }
  }
  delay(10);
}

Once upload program it to your Arduino, and open the Serial Monitor. Try sending the values to Arduino like ‘H’, ‘L’ or anything else. The LED should light up every time you send an ‘H’ and should turn off when you send a ‘L’.

Advertisements

Getting started with Arduino + Pyhton

Learning Python opens many doors for any newbie. As codeacademy helped me realize my dream to learn Python, I want to use this knowledge to make some embedded stuff. The first think that came in my mind was Arduino. Yes, anyone who is familiar with Arduino & want to apply his/her Python skills then this post may be beneficial for them.

Hardware requirement

Arduino UNO or Arduino Atmega328 will be enough to get started.

Software requirement

Here come little tricky part. I hope Python is already installed on you system (Windows/Linux). Since python will communicate over serial there is an addition package you need to download that is called Pyserial, as the name seems obvious. Windows users can run setup directly, Linux users can too. But Linux users must be searching for command so I may ease their work following these steps:-

1. Open terminal -> Get super user access -> user password

sudo su

2. You can use any one of below two methods

pip install pyserial

or

easy_install -U pyserial

3. That’s it, you are done with installation part. Now open you Python IDE (windows user) and Linux users (terminal -> idle)

idle

4. Python shell -> file -> New window -> save as -> blinkled.py ->

Image

ImageImageImageImage

 5. These two lines will import necessary modules from library.
     Now this is tricky part, how to connect to serial port. Go to device manager (Windows) & find in ports. But for Linux users           there is a command to find out COM port to which Arduino is connected.
dmesg | grep tty

it will show COM port to which device is connected. Now you need to assign a variable in Python IDE for arduino board,             name it Arduino itself. Now you code will look like this.

Image

You should change the COM port, if it is connected to different port.
Note: Be careful with the Baud Rate. The baud rate mentioned in Python IDE should be same as the baud rate chosen for Arduino serial monitor. For example, some Sonar sensor libraries talks with Arduino on 115200 baud rate (Better to take care of it rather than scratch your head for hour when things doesn’t work).
6.  Now finally if you are familiar with Python then you can get whole idea of how code is written & how it is working. Final coding looks like this-
Screenshot from 2013-10-22 13:32:32
7. Finally run the program from    ( run -> run module) or you can press F5 that will prompt to save. Save & view the Python         shell. It will look like this-
Image
Troubleshooting
But wait, what is going on their? The LED (Pin 13) is not blinking what is going on? Python code seems to be working fine but what is the matter with Arduino board?
Well, well, well here is the matter with the stuff. Python is sending some string ‘H’ and ‘L’ over serial pin but Arduino is not able to understand what to do with these command. Perhaps I need to program Arduino first with instructions that tell it to make LED high when it receive ‘H’ over serial & make LED low for ‘L’ .  Follow my other post to do that. After uploading the program, run your saved Python program & all things will work fine. DONE!!!!!
Source Code
You can directly use these codes into your Python IDE.

import serial
import time
Arduino = serial.Serial(‘/dev/ttyACM0’, 9600)
time.sleep(2) # waiting the initialization…
print(“initialising”)
while True:
Arduino.write(‘H’) # turns LED ON
print(“LED ON”)
time.sleep(2) # waits for 2 secondArduino.write(‘L’) # turns LED OFF
print(“LED OFF”)
time.sleep(2) # waits for 1 s
Arduino.close() #say goodbye to Arduino

Embedded Hardware Development

Few quotations to start with…

Today I’m going to start after a long break, as I was busy with projects. I was figuring out the dots of my journey to learn flow of Embedded System development from A to Z. Learning gives you thinking & thinking gives you quotations to help future seekers. Some of my technical quotations as a result of my learning are-

People use computer to find answers, I use computer to find questions.

If there is hardware issue there must be tangible misbehavior, if there is software issue there must be intangible misbehavior.

Overview

When I started my journey in 2006 I had little or no idea of what is Embedded System. However I came across Arduino during its early age in 2008 but I didn’t think it will be so much popular in future. As of that time there was no anything like open hardware. If you want to do something from scratch then you should have proper guidance & need so much hands on. In late 2011 I get to know about Arduino & open source hardware, then I realized that working on hands-on & open source is so much important if you want to learn Embedded hardware development.

You need to do it your own

In between these years from 2009-2011, I was victim or either start from scratch or reverse engineering. I have not finalized my initial project till 5 years. However my project was designed by someone else but later when I got that project it was not working according to my needs. So I got motivation to make the dead alive & taken the path of reverse engineering.

Reverse Engineering

Reverse engineering gives many advantage over working on finished development board if you want to be a hardware developer. I tried my hands with prototyping basic freeduino board & then I got idea of basic Embedded electronics. I got the idea of how crystal oscillator works, how reset pin works & it seems so easy because embedded electronics works on digital part of the discrete components, it is so easy with respect to learning Analog electronics. Rest of all thing is programming.

Hardware Software rivalry

In my early learning path I was hardwired by the fact that there is no need to learn software for a hardware guy, but later I realized it is a myth. The fact is that any efficient engineering work these days requires knowledge of programming. In terms of hardware-software : “A software engineer must know how hardware works, whereas a hardware engineer must know what are the problems that a software guy may face during firmware writing”. In this sense I can say that hardware guy needs to have more technical skill than software.

Programming skills are as important as oxygen to live but so simple to start

Arduino becomes a good start for learning programming skills. It has very much advanced IDE & doesn’t require any external JTAG programmer. Programming language is OOP type & uses wiring library. Coding style is so simple & requires few lines of code that any novice can get the idea. If you are still thinking that someday I’ll start coding, wake up now, there is no someday. Work on Open source hardware or at least get basic skills from Codeacademy.

Projects Projects Projects

Work on as many projects as you can. Learning something is never easy & you can’t get whole idea by working on only one project. That is what companies ask for- “Experience”. Work on open projects & get the idea of how hardware working with peripheral components. Get the idea of protocol implemented for interfacing such as SPI, I2C, USB, RS232. Knowledge of protocol is highly important for embedded hardware development.

Hardware Development tools

Knowledge is worthless if it can’t be implemented. Just like a cricketer is powerless without bat & ball, hardware developer is powerless without tools. Tools can be categorized in two parts hard & soft tool. Hard tools such as soldering station, pliers, cutter,  multimeter, discrete components, perfoboard, PCB etchant (added plus upto double layer PCB if you don’t want to send you design to fab house). Soft tools such as simulator (Orcad schematic capture, LTspice, Proteus), PCB designer (Eagle, Proteus, cadstar), optional advanced tool for Signal Integrity analysis (Flotherm, Mentor graphics tools). I am not going to give details of tools here as it requires detailed explanation.

Portfolio Project

It’s time to use all knowledge & skill to work on my own project. Get start with selection of micro controller first. I have selected ARM LPC1342 CortexM3 processor for building my own prototype which will cover all basic peripherals I want to implement.

Project details will be updated in separate  post.