Posts Tagged arduino

def Python meets Arduino:

I write this article as an introduction to how the Python language can be used to talk to the Arduino board. In essence, what I do here is simple serial communication.

There is a program that I ve written for the Arduino board that does two simple things:

  • Reads the serial input.
  • Constrains the input to an integer between 0 and 179 and writes it to the servo motor.

The point at which python comes into play is when we, as users, provide the input. Although the Arduino software has its own Serial monitor which can be used to accomplish the same task, I am using python just as an alternative (this can be done in any other language too…python is just the easiest one). It might so happen that later on the Serial monitor and its functionality  might not be enough. You might be writing a larger application of which the serial communication is only a small part. In which case Python to the rescue…

So here is the scenario in short..

There is a servo motor attached to one of the pwm digital output pins of the Arduino and i have the python interpreter running on my computer. If you don’t have python installed on your comp you can install it from You will also need to install the pySerial module if don’t already have it. Open the python interpreter and type “import serial”. If you get an error install it from here

Next I upload the following code to the board:

Serial available() : returns the number of characters still left to be read

One more thing to remember, in serial communication information is received in terms of character bytes. That means even if we give the input as the number 123. The microcontroller receives it as character ‘1’, character ‘2’ and character ‘3’ respectively

The next statement takes care of this problem. The expression ” incoming_byte – ‘0’ “, subtracts the character ‘0’ from the character received  giving us an integer for e.g. ‘1’ – ‘0’ = 1 (integer). and adds it to 10 * sum (as calculated in the previous iteration). The final sum is constrained between 0 & 179 and assigned to the variable “angle”. Finally, the value of “angle” is written to the Servo.

Once the program is uploaded, I move on to python. I open the interpreter and type the following commands which are quite self explanatory:

So…thats it. One of the simple ways in which python can be used with Arduino.

, , , , , , , ,


Android controlled Arduino bot

This is a project which me and a friend of mine ( attempted long time back.

It is a robot which is controlled by a cell phone.

It makes use of the Arduino and the android platform. It’s a simple bot which has an arduino board, powered by the Atmega328 microcontroller, as the brain. However the microcontroller receives its command from an Android phone.

The inspiration for the project was to acquire the ability to affect the physical environment around us using a device to which everyone has access to ….Cell phone.

The following components were used in making it:

  • Arduino boards
  • RF Transmitter/Receiver
  • L293D motor driver
  • Motors
  • Laptop
  • Android phone

So….this is how it works..

The first stage of this project is to have a graphical user interface with which the people can interact. As the robot, at this stage, only needs to maneuver in two dimensions, the GUI is not as impressive as it would need to be for more advanced applications. Currently it is just a group of 5 buttons namely…front, back, right, left and stop.

The real action is however the one that cannot be seen. This was the tough part actually. Since none of us was well versed with java and Android programming, it took a fair amount of searching and learning in order to get the communication set up. Right now only the bare basics of communications have been included in the project.

When the application is run, it first checks to see if the Bluetooth on the phone is enabled. If not it asks permission to enable it.

Next the list of the devices with which the phone had previously paired with is presented. Since the app is in a very crude stage, the ability of searching for new devices has not been included.

Once the device is selected it establishes a link with it. Here the other device with which the phone communicates is my laptop. The laptop acts as a server and has a python script running which bridges the link between the Bluetooth and the USB serial port.

Once the link is established, the user can press one off the buttons to maneuver the bot. Behind every button click there is a listener which then takes a byte code representing that button and sends it to the server.

The server is a Python script running on the laptop. It is nothing but a tunnel which directs the input from the bluetooth to the serial port.

The server then forwards this number to the serial port.

The serial port has an arduino board attached to it which is further connected to the RF transmitter.

The transmitter was in turn attached to another arduino board. To work with RF communication we used the VirtualWire library which is very well documented and made the job really easy.

The transmitter then forwards this code to the receiver on the bot. On getting the data from the RF receiver the microcontroller calls upon the appropriate functions which set the inputs of the Motor driver ic L293D to HIGH & LOW values correspondingly to maneuver the robot.

As you might have noticed, we had to take the longer route here. Since we dont have a bluetooth module for direct contact with the arduino, we had to use the laptop as an intermediate (with the module the laptop wont be required at all).

You can check out the video of what we ended up with at: Here ‘s the video

, , ,

1 Comment

Intensity control of an LED using LDR and Arduino board

This is another simple project that you might want to try out using your Arduino board.

So what is exactly our goal here?

As the title says, we are going to control the intensity of an LED(Light Emitting Diode) using an LDR (Light Dependent Resistor).
But more importantly we are going to look at how we take in an analog input, make sense of that input, and output what appears to be analog signal ( …but it ‘s not !!..its actually PWM) using  an Arduino board.

Some Theory

Okay it might be possible that there were a few terms in the above paragraph that might be new to you. In case you have completely understood it, you may skip ahead but in case you haven’t this might clear it up a bit.


It stands for “Light Dependent Resistor”. It ‘s a type of resistor whose value changes with intensity or the amount of light falling on it. As the amount of light falling on it increases the its resistance decreases and the other way round. Understanding why it happens will require going into the depths of the material with which it is made and this isn’t really an article for that. However there are many articles which will solve the question but for now you only need to remember that it ‘s resistance value changes with the amount of light falling on it.

Analog to Digital Converter:

It takes in an analog value( in this case the voltage across the LDR) and converts it to a 10 bit digital. Since we have 10 bits, the output of the ADC ranges from 0-1023 (i.e. 1024 values 2^10).  Every voltage between 0 – 5V is mapped to a value between 0-1023. Why till 1023? The reason is the resolution of our ADC. Here we represent every value with a combination of 10 bits. As the number of bits used to represent a value increases, the output value gets much more accurate. The following figure gives an idea of what happens in an ADC using 2 bit resolution.

Value         Bit Representation

0 – 1.25             00

1.25-2.5            01

2.5-3.75            10

3.75-5                11


Pulse Width Modulation is a technique in which the width of the pulses can be varied according to the message signal. In this case the led is connected to a PWM output pin of the arduino board and using the analogWrite() function we make the width of the pulses on the pin vary. Varying the width of the pulses can be seen as varying the average voltage on the pin. So although we are only changing the amount of time for which the led is on or off, due to the persistence of vision it is as if we are varying the voltage across the led which causes it to fade and brighten up.

How do we get there?

Okay so lets get started. We are going to require the following items:

  • Resistors
  • Light Emitting Diode
  • Light Dependent Resistor
  • Arduino board
  • Connecting wires
  • Breadboard

The connections are as follows:

  • Connect one end of a resistor ( I have a 1k one…you can use any other value if you prefer.. just make sure it is not too high or too low) to the 5V pin of the arduino board. Connect the other end of the resistor to one end of the LDR. The other end of the LDR should be connected to the ground (GND).
  • Take a wire from the junction of the resistor & the LDR connected earlier and connect it to the Analog Input 5 on the arduino board.
  • Next take a wire from any of the PWM pins on your board and connect it to the Anode of the LED. Connect the cathode of the LED to one end of a resistor with the other end connected to the GND.

Those are all the connections we have to make.

Next we need to write a program to make it work.

The program is quite self explanatory

Some things to note are

  • When I uploaded the program to the board I found that the value in the variable “value” varied over a range of 990 – 1010. This is a very small range in comparison to 0 – 1023. So I constrained it between 990 – 1010 before mapping it to 0 – 255. If I had not done this  the value written to the ledPin would have varied over a much smaller range and this would not have been noticeable in the varying brightness of the led.
  • I have also put in the Serial.println() function. It ‘s a good idea to use some of these when you want to check the values being read or written. Helps in debugging.

    The circuit in sufficient light

Now when I cover the circuit the led begins to glow. Its brightness increases as I continue enclosing the LDR more.Until the point..

In the dark

So…there you go controlling the intensity of the LED using an LDR.

You can check out the video here: Here ‘s the Video

Credits google for the first two pics.

, , , , ,

Leave a comment

Arduino 101 – Blinking LED

HI….If you are reading this page, i assume that you either have an Arduino microcontroller board or are looking to get one. Well ..for those of you who are new to microcontrollers and are looking to get ones board for themselves, let me tell you that Arduino is the perfect way to begin. It uses the Atmega microcontroller and is very easy to use. Also there are tons of Arduino projects posted on the internet which you can and should check out on your time.

Ok…so lets begin..Some knowledge of any of the programming languages out there would make things easier for you to understand but its not the end of the world if you don’t.

In this post i ll tell you how to get an LED blinking with the help of your Arduino board.

In  order to do this you ll require the following items:

  • An Arduino board.
  • USB cable
  • Resistor
  • LED
  • Some single strand wires
  • Bread board

The circuit for this implementation is quite simple.

Connect the pin3 to the anode of the led. The cathode ( the leg towards the blunt side when you look from the top ) is connected to one end of the resistor. The other end of the resistor is connected to the GND pin on the board.

Now that you have all the components, the next step is to download the arduino software from this link:

Once you are done with it, start the application. The interface is really simple to understand. There is a text editor, where you type in your code, and a few button above that let you verify and upload your program to the board.

The following figure shows you the code that we have used here.

Lets go through the code line by line.

int led =3;

This line declares an integer variable led and we assign to it the digital pin number that we are going to use. (In this case the 3rd digital pin)

void setup(){

pinMode(led, OUTPUT);


This “setup” is a function/method which runs, only once, whenever you load a new program onto your board or whenever you “power on” the board before moving to the loop() method. Also it returns nothing (“void”). In this method we define all the stuff necessary for a program to run properly eg. configuring the pins as inputs/outputs, setting the baud rate etc. (Here in the setup() method we define our int variable “led” as an “Output” using the pinMode() method,  since  well…it is gonna be our output pin)

Next we move on to the loop() function.

This is where you write the code that you want your microcontroller to follow. The loop function repeats itself as long as the board has power (that s why the “loop”). As mentioned earlier the loop() function executes after the setup() function.

In this example we want our LED to blink continuously…so here s the code that makes it happen:

void loop (){






Remember that the variable “led” is used to denote the 3rd digital pin here. So the first line tells the microcontroller to set the voltage on that pin to “High” value or 5V. This action forward biases the LED and it starts emitting light.

Next is the delay() statement. The argument to this function is the number of milliseconds it must wait before executing the next statement. Here the microcontroller waits for 500 ms before setting the voltage of the pin to “Low” or 0V which turns off the diode. This is followed by another delay of 500ms.

And then it again goes to digitalWrite(led, HIGH); And so the cycle continues.

Once we are done typing in the code, we ll need to verify it. This is done by clicking on the button which has a tick mark on it (refer to the figure). If everything s all right we need to upload the code to the board, but before doing that make sure that you have selected the correct board and the port. In order to check which COM port the board is using go to Start -> Right click on Computer ->  manage -> Device manager. You will see “COM” in the list. See the one that your board is using and then in the Arduino window go to Tools and select your board and the COM port that you just verified.

Now for the last step….click on the Upload button (the one with the arrow, next to the verify button).

The result is … we have an LED that stays on for half a second and turns off for the next half repeatedly…hence a blinking LED.

, , , ,

Leave a comment