Saturday, January 14, 2023

Program Arduino with Python | PyFirmata |

Before we dive deep into this topic it is very important to understand very basic of Arduino- like how to upload a program from Arduino IDE and how to write simple line of codes just to turn ON/OFF Led is good enough. If you want to learn how to do it, please checkout these 2 posts: Post 1 and Post 2.

So, let’s start today's topic:

Connect your Arduino board to your computer and then go to: File-> Examples ->Firmata -> StandardFirmata

It should open the StandardFirmata program on a new IDE. As soon as it opens just click on upload button and wait until it is completely uploaded on your Arduino UNO without errors. If you get any error write it in comment section I will try to help as much as I can.

So, what we did right now is uploaded a sketch on Arduino's memory so from now if we send commands to Arduino from Python, Arduino can understand them. 

Now we can start with the python part. I personally like using PyCharm as my default python IDE, so in this video we will use the same. When you open PyCharm click on Package installer in bottom left side of the IDE which looks like this:

Now type pyFirmata in the search console of Package installer and you can see an available pyFirmata package there:

Once you locate the right package as shown in image above you should be able to install in couple of seconds without any problem.

If you are using any other IDE just write this in your terminal :

pip install pyfirmata


Now as we have installed the package for PyFirmata what we need to do next is import the package in IDE by writing:

import pyfirmata

then we can define the Arduino board port. You can find the COM port number by going to Start-> Control Panel -> Hardware and Sound -> Device manager and after that look for “Ports” and see what port Arduino is connected to for me it is “COM7”, please check for yourself.


Now we need to write our port number in python code by writing: 

Arduino(‘COM7’)

Now we can define pin number we are interested in by writing:  

Pin3 = board.get_pin(‘d:3:p’) 

Where "d" means digital pin and then "3" is pin number and last "p" means PWM control. After this you have configured the pin now the very last step is to try manipulating values on the pin to do so try:

Pin3.write(0.1) 

In the brackets you can write anything between 0 and 1 try couple of things and then see how the Arduino responds to that. 

So that was all you needed to know to start getting hands on with python and Arduino if you want to know more about available commands and stuff pls refer to the documentation in the description. 

(https://pyfirmata.readthedocs.io/en/latest/


Stay tuned 😊


What is PWM? How to control voltage using Arduino?

 


In this blog we will try to understand what PWM is and how it works before we start doing some exciting stuff. PWM stands for Pulse Width Modulation. Which in simple words means we reduce the power delivered by an electric signal by chopping it down into discrete part. It is just switching On and Off of the power supply at a very fast rate resulting in controllable average voltage. Don’t spin your head around let’s see an example:

Image source: https://docs.arduino.cc/learn/microcontrollers/analog-output 

Assume we have a supply of 5 Volts

In the first case when duty cycle is 50% means the switching is done in such a way that 50% of time it is on and other 50% is OFF which will give us around => 0.5*5V= 2.5 V

In the same way in second case 75% duty cycle which means 75% of the time it remains On and other 25 % of the time it remains OFF which will give us around => 0.75*5V= 3.75 V 

And for the last case we have a duty cycle of 25%. Try to find out how much output voltage we will get out of it and write your answer in comment section.


Just to be a little clear here a 100% duty cycle has 256 resolutions (switching) which means when we say 50% duty cycle it means 128 resolutions. We can control the switching at as low as 1 resolution using PWM which means around 0.4 % duty cycle trust me that’s a lot of control. 


Now we will try to do it practically using an Arduino UNO just to see what it means and how it looks. You just need to know very basics of Arduino which I have explained in my last blog post. So, an Arduino can output a voltage of 5 Volts max in general. Which means when we send a command of ON on digital pins, we output 5 Volt from that pin. 

Photo by Harrison Broadbent on Unsplash


But maybe you have noticed there is tilde “ ~ “ sign on some of the digital pins and that denotes that the particular digital pin can be used for PWM. Just for little clarification Digital pin number 2, 5, 6, 9, 10 and 11 are PWM pins. Let’s start with pin number 11. So, we need to connect our Arduino UNO with a computer first which has Arduino IDE installed in it. If you haven’t done that, please refer to this blog post make sure you follow all those steps in the video to get your first program running on Arduino IDE. After you are good to go just initiate the pin by using:

void setup(){

  pinMode(11, OUTPUT);

  }

Now we need to write in the loop for this time we gonna use analogWrite instead of digitalWrite because analog write gives us opportunity to send analog values between 0 and 255 resolutions. So the syntax is:

 analogWrite(pin_number, resolution)

resolution can take any value between 0 (always OFF) to 255 (always ON). Now we can try following code after void setup():

 void loop(){

  analogWrite(11,20);

  delay(500);

  analogWrite(11,80);

  delay(500);

  analogWrite(11,120);

  delay(500);

  analogWrite(11,160);

  delay(500);

  analogWrite(11,200);

  delay(500);  

  analogWrite(11,255);

  delay(500);

}

What we are trying to do in this code is increase the resolution from 20 to 255 to see how the led lights up to see the effects in reality just insert a LED between pin number 11 and GND as shown in image below.


And on Arduino UNO click on upload and see how the LED reacts to the code.

Complete code: 

void setup(){

  pinMode(11,OUTPUT);

  } 

void loop(){

  analogWrite(11,20);

  delay(500);

  analogWrite(11,80);

  delay(500);

  analogWrite(11,120);

  delay(500);

  analogWrite(11,160);

  delay(500);

  analogWrite(11,200);

  delay(500);  

  analogWrite(11,260);

  delay(500);

}


If you want to do it in a little smart way just add a loop so we don’t have to re-write the commands again and again. To do so we can simply use: 

int i;

void setup(){

  pinMode(11,OUTPUT);

  } 

void loop(){


    i = 0;

    while ( i <= 255 ){

        analogWrite(11, i );

        delay(10);

        i = i + 1;

    }


    i = 255;

    while ( i >= 0 ){

        analogWrite(11, i );

        delay(10);

        i = i - 1;

    }

}

If you have any doubts regarding these lines of code, just write in the comment section I will try my best to help you out there. So, this was very basics of how to control voltage using Arduino UNO's PWM pins.


Stay tuned :)  


Thursday, January 12, 2023

Basic of Arduino within 10 minutes

 


In this blog we will cover very basics of Arduino UNO you need to know to start doing some cool stuff with this small yet very amazing microcontroller. In this whole article we will try to explain how digital I/O pins work in Arduino UNO and what amazing things can we do with it etc. 

First things first.

What is an Arduino UNO ?

It is one of the very classic and famous product from Arduino. It is a small microcontroller board with:

  • 14 Digital I/O pins (& 6 PWM’s)

  • 6 Analog Inputs

  • 16 MHz ceramic resonator

  • A USB port

  • A power jack

  • An ICSP header 

  • And a reset button

Graphical user interfaceDescription automatically generated with medium confidence

Image source: https://store.arduino.cc/products/arduino-uno-rev3

But today we will try to cover basics so we won’t be talking much about couple of things in the list instead we will cover only these things:

  • 14 Digital I/O pins (& PWM’s)

  • 6 Analog Inputs

  • A USB port

  • And a reset button

And believe me these are the things which can cover up to 90-95 percent of things which you are willing to do with your little UNO board.


So, lets learn more things about Arduino UNO by doing it.


First, take your Arduino board and connect it to you PC or laptop using USB 2.0 Type A/B cable.


Now to begin doing things with Arduino we need to have an interface in our computer that can talk to Arduino UNO board. To do so go to the link (https://www.arduino.cc/en/software) and download the latest version of Arduino IDE. At the time of this video, we had the following version: 

After you have downloaded it follow the guided instructions in the setup. If you get any problem, write it in comment section and I will try to give solution to that.

As soon as you run the IDE it will show something like notepad very simple. Where some commands are already written. We press a “Ctrl+A” and then "del" just to remove all the lines of code.

Now we got an empty screen like below:

Before we start writing the code, go to Tools-> Board and select Arduino UNO there or any other Arduino board you are using. 

Next go to Tools -> Port and select the Port showing your board name.

If you have done these steps correctly it means we are communication to right Arduino board at right address. 

Now, 

First we can setup a pin as we want to make an LED glow using Arduino we need to tell the controller which pin we have LED located (for our case it is PIN 13 as it is locate to closest GND pin and we won’t have to add a breadboard). Also, we want to give output from controller to the LED so we will have possibility to use any Digital I/O pin. 

To setup the pin we need to write 

 void setup(){

  pinMode(13,OUTPUT);

  }


Where 13 is the pin number and OUTPUT is the functionality. Until now we have directed and informed the controller that we are interested in pin 13 but now we should tell the controller what to do with pin 13 to see an action. So, lets try to turn the LED ON which is hanging in between our pin 13 and GND.

So now, we write:


void loop(){

  digitalWrite(13,1);

}

Where 13 is again pin number and 1 means ON or HIGH whereas 0 means OFF or LOW. We want to see the LED working so let’s make it ON.

To ensure no compilation errors we can first click on Verify . If it goes well, we can click on Upload then these lines of code will be uploaded to our microcontroller memory, and it will run this code for infinite number of times as we have used “void loop”.

As soon as upload is successful we will see the LED on PIN 13 turns ON. Now to turn it OFF just write the same code but instead on “1” in digitalWrite we will write “0” there. 

void setup(){

  pinMode(13,OUTPUT);

  }

void loop(){

  digitalWrite(13,0);

}

Now, your LED has turned OFF. Lets try blinking it we will add a little pause between lines by using delay.

void setup(){

  pinMode(13,OUTPUT);

  } 

void loop(){

  digitalWrite(13,1);

  delay(500);

  digitalWrite(13,0);

  delay(500);

}


So, we have written the code just for blinking once but it’s been written in the loop so it will keep on repeating the pattern forever. I really hope this video have given you good insights of how things work on digital I/O pins. But in next video we will cover about PWM pins. These PWM pins give us major advantages when it comes to voltage control and stuff like that. We will try to understand PWM by using very classic way that is dimming and brightening the LED.

Stay tuned 😊


Program Arduino with Python | PyFirmata |

Before we dive deep into this topic it is very important to understand very basic of Arduino- like how to upload a program from Arduino IDE ...