Introduction: Introducing Arduinos

Hello everyone, it would be great if we can introduce you to electronics using neat device called Arduino. In this instructable we will use Arduino Uno board, few resistors, photoresistor, an LED, breadboard, jumper wires and push down button. We will show you overview of Arduino Uno board, how to download and install Arduino IDE and we will cover topics like LED blinking and fading, serial communication (UART), button debouncing, ADC - Analog to Digital Conversion and PWM - Pulse Width Modulation.

Arduinos are electronics boards developed for people who are not electronics major, children, artists, hobists, and even for electronics professionals. Now, what can you do with Arduino? Well, you can design your own toys, automate something boring, like creating self-driving vacuum cleaner or lawn mower, you can create robots of all kinds, etc. Possibilities are endless. People even created small satellites for space exploration, how cool is that.

Arduino is electronics device designed to bring you power of microcontroller, but without complexity of low level microcontroller programming. What do we mean with this? If you buy single microcontroller chip you also have to buy dedicated programmer circuit, some resistors, quartz, capacitors and then you have to learn about internal structure of that microcontroller so that then you can start learning programming for that microcontroller. This process is long and hard, and for many very difficult, and if you are not electronics major, almost “impossible” and sometimes very expensive, because you can easily break components if you are beginner. That is why people invented Arduino, and boards like it. The heart, or the brain of every Arduino is a microcontroller. For basic ones it is an AVR microcontroller. A microcontroller is integrated electronics circuits which is used to control some application. It is computer on chip. It has CPU, RAM, flash memory, EEPROM (Electronically Erasable Programmable Read Only Memory), peripherals (I/O pins, ADC - Analog to Digital Converter, Timer/Counters, etc.) Arduino is an open source platform, which means that user can use and modify Arduino source code without legal obligations to the manufacturer (you can read more on this on Arduino official site.

Today, Arduino has a large community. If you are planning to build something with it, chances are that somebody already built it. So if you need help, you can search for solution very easily.

In this Instructable we will introduce you to electronics using Arduino and Arduino IDE. You should have to know how to program, how to use variables types, loops, if-else, etc. in order to finish this Instructable. Here are the list of electronics devices and parts that you'll need to have for this Instructable.

If you already own Arduino Uno, we recommend you to download our free ebook Uno R3 available on several languages. We also have free ebook for breadboard and jumper wires. In books we cover how to setup everything before using a board, and there are many link references for online help.

Step 1: Introducing Arduino Uno

Overview of the board

1. Microcontroller

The brain of this board is 28-pin microcontroller ATMega328. It is placed in 28-pin DIP socket, so that it can be easily replaced. The brain of an microcontroller is Central Processing Unit - CPU, and it controls everything that goes within microcontroller. It fetch program instructions stored in flash memory and execute them. This might involve taking data from working memory (RAM), work on it, and then putting it back, or for example it may mean changing one of the digital outputs from 0V to 5V.

2. Power supply

Board can be powered via usb connector or external power supply socket. Power regulator regulates whatever voltage between 7V and 12V is supplied from the power socket into a constant 5V that microcontroller need’s.

3. Power pins

Power pins consists of 8 female header pins. First is NC - Not Connected to anything. Second is IOREF and it is used for shields to determine with what voltage level Arduino board works (3.3V or 5V, for Arduino Uno it is 5V). Third is RESET pin and when we connect it to 0V, program in microcontroller will be on reset which means it will start again from beginning. Fourth pin is 3.3V power supply pin, and fifth pin is 5V power supply pin. Sixth and seventh pins are GND - GrouND = 0V. And eighth pin is Vin, this pin can be used for regulated external power supply, and for Arduino Uno you can connect this pin to 5V to power the board.

4. Analog input pins

Analog input pins consist of 6 female header pins labeled A0 - A5. These pins can be used to measure voltage level connected to them. For example, moisture sensor output, or distance sensor output, or temperature sensor output, etc.

5. Digital Input/Output Pins

Digital Input/Output (I/O) pins consists of 18 female header pins, broken in two parts. First 14 of them labeled 0 - 13 are digital I/O pins, and the ones with tilde sign (~) in name can be set as PWM output (more about PWM later). Fifteenth pin is GND. Sixteenth pin is Aref pin, Analog reference pin and it is used as reference voltage level for Analog to Digital Conversion (ADC, more about it later). This pin in connected to 5V for Arduino Uno. Seventeenth and eighteenth pins are SDA and SCL pins, and these pins are used in Two Wire serial Interface (we won’t cover TWI in this Instructable). Digital I/O pins 0 and 1 are also labeled as Rx and Tx (Receive and Transmit), which can be used for serial communication (serial or UART interface, more on this later). Don’t use these pins as digital I/O pins, because they are used when microcontroller is programmed.

6. Onboard Light Emitted Diodes - LEDs

One orange LED is connected to digital I/O pin 13. One green LED is on when board is connected to power supply, and off when it is disconnected. Two orange LEDs are connected to serial communication lines (Rx and Tx) and flashing when serial communication is in use (usually when we program microcontroller).

7. Onboard programming circuit

Arduino Uno board have another microcontroller onboard, and it is Atmega 16U2. This secondary microcontroller is part of USB-to-serial circuit, that is used for programming main microcontroller. This Atmega16U2 acts as bridge that converts signals from serial interface (UART interface) of the microcontroller and signals of USB-bus. Because of this and pre programmed bootloader in both microcontrollers we are able to connect and program our Arduino Uno board with computer.

8. Connecting Arduino board with computer

To connect Arduino Uno to your computer you need USB A to B cable. When you buy any Arduino Uno board from AZ-Delivery we ship cable with the board. But be careful, some boards are shipped without cable. So pay close attention when you buying electronics.

Step 2: Downloading and Installing Arduino IDE

IDE stands for Integrated Development Environment, and that is an app like word processor where we write our sets of instructions (programs) for microcontroller. This app converts our code into machine language that microcontroller understands, and then we upload it to microcontroller.

To download Arduino IDE go to official Arduino site and under software tab, click on downloads. There is option for Arduino web editor. You can program your Arduino board using only your browser and Arduino web editor. More on this here. There is also option to download app and install it on your local machine, go on downloads page find your operating system and download file for your operating system.

For windows, we recommend you to use windows 10 because it installs drivers on its own, so you don’t trouble yourself with it. We also recommend if you are using windows to download zip file for non admin install - it is much easier to install that way. When you download zip file, unzip it and go to unzipped folder and open arduino application. And that’s it, you are ready to go on next step.

But if you are using older version of windows, go on this link. to find how to install drivers.

For linux, choose your computer architecture and download appropriate file. Extract it, and go to extracted directory then open terminal and write ./arduino . This is how you will open Arduino IDE and you are ready to go to the next step.

For Mac OS X you have to follow this guide on how to install Arduino software.

Step 3: First Program - Heartbeat

Connect your fresh bought Arduino Uno to your computer. You’ll see green LED on and orange LED (connected to digital pin 13) blinking. If you used your Arduino Uno before, orange LED may not blink (depending on application of your use). Now we are ready to continue.

When you open Arduino IDE, in order to setup IDE properly go to: Tools > Port > …… and choose port with Arduino Uno name.

For windows it should be something like “COM4(Arduino/Genuino Uno)”.

For linux it is something like “/dev/ttyACM0 (Arduino/Genuino Uno)”.

And for OS “/dev/cu.usbmodem1461(Arduino/Genuino Uno)”.

Then go to Tools > Board > Arduino/Genuino Uno and choose your board name.

Before we continue let’s explain few things. First, every program example, even program you wrote is called a sketch. In Arduino IDE there is a big list of sketches for almost everything we are going to show you. So when you want to do something and you don’t know how, you should go throughout this list of sketches, you’ll probably find a sketch with program example. And every sketch is commented so well, so that every action is explained.

Now, let’s upload our first sketch to Arduino Uno board with first program example. Go to: File > Examples > 01.Basics > Blink. A new IDE with Blink sketch will open. In this sketch we are using one LED and turn it on and off. Because this is a way to check if everything working fine, this sketch is usually called “Hello world” example. When you upload this sketch to board, this is when your Arduino says: “I’m alive, hello world!” :-) . There are a lot of comments in sketch, read them, usually they explain everything ;-) .

But we are going to change it a little bit, because to just turn on/off an LED is boring. Let’s simulate a heart beat. We deleted all comments because of simplicity. Change a loop function like this and leave the rest as it is:

<p>void loop() {<br>  digitalWrite(LED_BUILTIN, LOW);    <br>  delay(1100);                      
  digitalWrite(LED_BUILTIN, HIGH);   
  digitalWrite(LED_BUILTIN, LOW);  
  digitalWrite(LED_BUILTIN, HIGH);  

Now, go to: Sketch > Upload, or press Upload => icon to upload.

And if everything went well you should see onboard orange LED blinking in heart beat manner.

If you using Linux and you encountered error like “Error opening serial port...”, you should read this.

Now let’s explain the sketch. In almost every sketch there are two functions: setup() and loop() function:

  • setup() function runs only once, at the beginning of program execution, when you power up the board, or when you reset the board. In this function we setup all initializations, for example declare the state of digital input/output pin that we use. In this sketch, we declare that digital I/O pin 13 (LED_BUILTIN) is digital output pin. Here, we also initialize all stuff that we need, for example setting up analog input pins, or serial interface for serial communication, etc. (more on this later).
  • loop() function runs after setup() and it runs indefinitely, over and over again, so called “endless” loop function. Actually it runs all time while the board is connected to the power. This is because programs in electronics devices should never reach the end, because if that happen that means your device is as good as turned off. In this sketch we set digital pin 13 to ond, then we set digital pin 13 to low value (0V or GND; LED OFF) then delay that or wait for 1100 milliseconds, which is more than a second, then we set digital pin 13 to high value (5V; LED ON), and wait for 150 milliseconds. Than OFF for 100 milliseconds and again ON for 150 milliseconds. And this is repeated endlessly.

Step 4: LED Fading

For purpose of this sketch we will use external green LED with 330 ohm resistor. Load the sketch: File > Examples > 01.Basics > Fade.

Before we continue we should turn on line numbers if that is not already turned on in Arduino IDE. Go to: File > Preferences. A new Preferences window will open. Check the box “Display line numbers” and then click on OK.

Now we can continue. On lines 16, 17 and 18 three variables are defined. First is “led” and in it we store number of digital I/O pin that is connected to the LED. In this sketch that is 9. Other two variables are for changing brightness of LED, first is level of brightness and second is number that represents length of increase/decrease step between two levels of brightness.

In the setup() function we declare state of digital I/O pin 9 to output. Nothing new, isn’t it.

But in the loop() function we have few new things. First is analogWrite() function which defines PWM “voltage level” of digital I/O pin (more on PWM and analogWrite() later). In this case that is level of LED brightness (LOB). Then the rest of loop() function is algorithm for changing brightness. This should be self explanatory. If not, let’s go quickly through it. On line 32 we add to current LOB a step value, then ask if LOB is less than or equal to 0 (LOB can not be negative value, because, you know, PHYSICS) and if it is more than 255.. But wait, why 255? Because resolution of PWM output is 256, from 0 to 255 values. This is used in analogWrite() function, so that voltage level is represented in steps form 0 which is 0V to 255 which is 5V (we will cover this in more detail later). If this condition is not met, then program continue execution to next instruction which is delay(30). This delay() is just to slow down this process because it is too fast. You won’t see LED fading effect without this line of code... But if condition is met, then we change LOB step sign to negative, so that LOB start to fall down. When next time this condition is met, then negative LOB step will be changed to positive and so on, in the infinity. If you wish to slow down process even more, just change 30 in delay(30) to some other higher value.

We are using LED and resistor for this example, so let’s explain what a resistor and LED are.

First resistor, it has two legs for connecting into electronics grid and it is used for limiting current in that part of electronics grid (direction of resistor does not matter, it will do the job in both ways). We use a quarter of Watt resistor. This is power of resistor, and it means when current flows through a resistor, resistor emits heat. The greater this current flow the hotter the resistor will get. Resistors are rated by the value of their resistance and the power given in watts (W) that they can safely dissipate based mainly upon their size. The greater its surface area (the bigger the resistor) the more power it can dissipate safely into the ambient air or into a heatsink.

You can think about it like if you are using water hose to water your plants, and you squeeze hose with your hand, you are limiting water flow in hose. In this example your hand is resistor, water flow is current and volume of water is voltage.

Now, diode, it has two legs for connection into electronics grid, anode and cathode. Diode is electronics part that enables flow of current but only in one direction from anode to cathode. If you reverse it, there won't be current flow, but you could destroy diode if you exceed current and voltage limits.

LED - Light Emitting Diode is a special diode which emits colored light when current flows through it (longer leg of LED is anode, and the other leg is cathode). Diodes also can be destroyed when we exceed current and voltage limits, no matter of connected direction, so be carefull, read the datasheets. This is why we use resistor with LED so we can limit current flow and prevent LED damage.

Now, connect everything like on diagram and upload sketch to your Arduino board, and see the magic of LED fading.

Step 5: Button and LED

Now, let’s turn on/off an LED with a push down button or a switch. What is a button, and what is a switch? Well both are electronics parts, with two states, on and off, and both are used to turn on/off some part of electronics grid. But the difference is that when you change state of a switch it will stay in that state, BUT when you change the state of a button, it will stay in that state only for the time you are holding it in that state. When you stop pressing a button, it will return in its default state, which is in most cases off state. Some swithces have more then two states, but we won't use them in Instructables.

For this step we will use solderless breadboard. A breadboard is electronics part for prototyping electronics. With it we can easily connect and than disconnect other electronics parts without hard connections, no soldering needed. Breadboard pinout.

Make connections with Arduino Uno, breadboard, button, 10k ohm resistor, LED and 330 ohm resistor like on diagram.

As you can see, we have two schematics on the diagram, and they function separately. You can use any of these schematics to turn on/off an LED, because they are doing same function but in a different way. Arduino Uno is used just as stable power supply. Left schematics is with Pull Down resistor and right is with Pull Up resistor. You can see which is which, by looking at 10k ohm resistor and on what rail it is connected (two lower resistors on image). Pull Down means that a resistor is connected to the ground, and Pull Up means that a resistor is connected to the 5V, power supply. 330 ohm resistors near LEDs are used as LED protection. When we press a buttons, it will turn on corresponding LEDs. When we stop pressing buttons, it will turn off corresponding LEDs.
You may wonder, why all of this? It is simple, if you are using digital I/O pin as an input, you have to always connect digital I/O pin to the GND (pull down resistor) OR to the power supply (pull up resistor). You can never, NEVER, leave it unconnected, because you will get gibberish readings. Why? It is because, digital input pin will read voltage from surroundings, such as voltage levels from surrounding pins or static electricity voltage level from dust that surround the pin, etc. When you read unconnected pin you will get unstable value. We will show you these readings when we cover serial communication in the next step.

We will now read an input from a button on digital I/O pin 2. Connect everything like on diagram.

Go to sketch: File > Examples > 02.Digital > Button , and upload it to Arduino Uno. Press a button! Onboard orange LED will be turned on. Cool! Again, read the comments. This sketch is self explanatory, and we don’t need to go through it.

Step 6: Serial Communication

Now finally something interesting. With serial communication you can communicate with your Arduino Uno board. How cool is that? :)

What is actually serial communication? It is a way on which microcontrollers can talk to each other. Simply said, one mcu outputs (sends, transmits) a signal, high or low voltage pulses on wire that connect two mcus. The other mcu is in input mode, listens for voltage pulses, and act like receiver. So here we have a serial protocol, the rules for encoding data into voltage pulses and decoding the voltage pulses back into data. The most common serial mode is Universal Asynchronous Receive and Transmit or UART serial.

Here to send one byte of data we have several rules.

First we have to agree on speed of voltage pulses, how fast data is transmitted and received. This speed is called BAUD RATE and it is represented in bits per second. And its values can be: 2400, 4800, 9600, 14400, 19200, etc. It can go as high as 250000. Most common is 9600.

Second every byte of data has to have start and stop bit, so that UART can differentiate where data byte starts and ends.

Third we can adjust the length of data byte. (we won’t get into to much details on this) Default is 8 bits, but it can be also 9 or 10 bits.

Forth, there is parity bit at the and of data byte and before stop bit. This bit is optional, you don’t have to use it. It is used to check if there is error in data byte. (we won’t get into to much details on this).

Setting up serial communication with Arduino is so easy, just few lines of code. Let’s turn on onboard LED by command sent from computer using serial communication. For this you will only need Arduino Uno board connected to computer.

In Arduino IDE there is a neat tool for monitoring serial interface, called, you guess it, Serial Monitor. To start it go to: Tools > Serial Monitor.

Write or copy/paste this little sketch into IDE, and upload it in your Arduino Uno.

<p>void setup() {<br>        // setting digital I/O pin 13 as output <br>        pinMode(LED_BUILTIN, OUTPUT);
        // setting up serial communication - 9600 is baud (communication) rate, 9600 bits per second
void loop() {
  	// check if there is serial message on serial line
        // Serial.available() returns the number of bytes that are in serial buffer
  	if (Serial.available() > 0) {
    		// if it is, read it and store into letter variable
   		char letter =;
    		if(letter == '1') {
      			digitalWrite(LED_BUILTIN, HIGH);
      			// write back on serial line
      			Serial.println("LED turned on");
    		else if(letter = '0') {
      			digitalWrite(LED_BUILTIN, LOW);
      			Serial.println("LED turned off");

Now, we are going to explain few bits of this sketch. First, Serial.begin() function. It is used to setup serial interface for Arduino Uno. We do this by entering baud rate in parentheses, and in this sketch it is 9600.

Now, you have to setup listener for communication. Serial.available() function listens for messages on serial communication line, and when message buffer is filled with data, even with just one character, Serial.available() returns number of bytes in message buffer. We wait for this, and than take that message in message buffer, read it, and check it for specific value. When message is ‘1’ it will turn on an led, and write back message to the serial line “LED turned on”. To write to serial line we use Serial.println() function. You just write in quotes text that you want to print back and put in parentheses of Serial.println().

When you upload this sketch into Arduino Uno board, start Serial Monitor tool. Write ‘1’ and send it. And, now you are communicating with your Arduino Uno :)

Now, let’s use button from previous example. Let’s read digital input pin state, and send this to the serial communication, first unconnected, then with button and Pull Down, or Pull Up resistor. For this, we will use Arduino Uno, a button and a 10k ohm resistor.

When you upload a DIgitalReadSerial sketch (File > Examples > 01.Basics > DigitalReadSerial) to your Arduino Uno, and make no connections to anything, and start Serial Monitor, this is that gibbrish values that we wrote about. That is why when we read input pin state use Pull Up or Pull Down resistors.

Now, make connections so you can create schematics with pull up and pull down resistors and buttons, like on diagram.

You can do this twice, once for Pull Up and once for Pull Down in order to get results we are getting. You don't need to make both at the same time if you not desire it.

But we are going to make this work. We need to change DigitalReadSerial sketch a little bit, so that we can use both buttons. Copy this code into IDE:

<p>int pullUp = 2; //  button on digital I/O pin 2 - Pull Down</p><p>int pullDown = 3; //  button on digital I/O pin 3 - Pull Up</p><p>void setup() {<br>  	Serial.begin(9600);
  	pinMode(pullUp, INPUT);
  	pinMode(pullDown, INPUT);
}</p><p>void loop() {
 	//  read buttons states
 	int buttonUpState = digitalRead(pullUp);
  	int buttonDownState = digitalRead(pullDown);</p><p>  	//  make a message that will be sent to the serial line
  	String message = "Pull Up: " + String(buttonUpState);
        message = message + ". Pull Down: " + String(buttonDownState);</p><p>  	//  send the message
  	delay(1);        // delay in between reads for stability

Here, nothing is new, except the line String message = “Pull Up: ”... Here we are creating message that will be sent to the serial line in one go. First we are making a string variable called message, and then we add text to it. String() function converts integer value into string, so that it can be concatenated to message string.
Upload this sketch to the Arduino Uno, and start Serial Monitor. Output should be like:

Pull Up: 1. Pull Down: 0

Do you see the difference? When you read unpressed button with Pull Up resistor (PUR) it will read digital one, and for Pull Down resistor (PDR), it will read digital zero. When you press button connected to PUR, output in Serial Monitor will change to digital zero. And when you press button connected to PDR, output will change to digital one. When both buttons are pressed, output should look like this:

Pull Up: 0. Pull Down: 1

Step 7: Button Debouncing

As you can see from previous step, when you press a button, it will be read as series of ones (for button with pull down resistor). No matter how fast you press it, and release it, read values still will be series of ones but never single one(0000000111111100000000). Sometimes, there will be several value changes before this zeros turn into ones on button press (000001010011011111111111111). Or you are maybe a button GOD and you are able to make it, a single one per one button press xD. (little joke)

This is called bounce effect, and solution is debouncing algorithm. Button bounce is serious problem in electronics, and there are numeros hardware and software solutions, but for most sketches in Arduino IDE simplest solution is software debouncing algorithm.

We will use connection diagram with only one button and with pull down resistor connected to digital I/O pin 2.

Copy this code into IDE:

<p>// digital pin 2 has a pushbutton attached to it. <br>int button = 2;
// variable for counting button presses
int count_presses = 0;
// two helper variables, for detecting one physical press
int ones_count = 0;
int real_press = 0;</p><p>void setup() {
    // initialize serial communication at 9600 bits per second:
    // make the button's pin an input:
    pinMode(button, INPUT);
    // to print first value of count_presses which is 0
void loop() {
    // read the button input pin:
    int button_state = digitalRead(button);
    // check state of a button, check for digital ones when button is pressed, and count them
    if (button_state) {
        ones_count++;</p><p>    }
    else {
        ones_count = 0;</p><p>    }
    //  if number of ones is 10, you pressed button long enough, mark this with real_press
    if (ones_count == 10) {</p><p>        real_press = 1;
    else {
        real_press = 0;
    // if flag is marked, count that, because it is one actual physical press, and send that number to the serial interface
    if (real_press) {
    delay(1);        // delay in between reads for stability

Upload it to the Arduino Uno, and start Serial Monitor. You should see numbers counting up when you press a button.

All this code does is counts button presses and prints it on serial communication line. Code is self explanatory.

Step 8: ADC

ADC stands for Analog to Digital Converter or Conversion. Why we need this? Well, our microcontroller in Arduino Uno is digital device, which means that it works only with digital signals. If we think about voltage, voltage digital signal can have two levels: digital zero or 0V and digital one or 5V. But, almost everything else work with analog signals, signals that can have any level, voltage in between 0V and 5V. You can see this on image.

So in order to make our microcontroller understand analog signals, we need to have some converter, and this is where we need ADC.

Aref pin on Arduino Uno is used as voltage reference pin for analog conversion. This voltage reference level MUST be as stable, accurate and noise free as possible in order to get accurate reading on ADC. Why do we need this? Well ADC can convert voltage values in between 0V and voltage level on Aref pin, which is in most cases 5V. But this value we can change depending on application. Aref = 5V is default setting for Arduino Uno, but using function analogReference() we can change this. (we won't use it in this Instructable)

So how actually ADC works? Well in the heart of the ADC is actually DAC - Digital to Analog Converter, and comparator. Comparator is used for comparing two values, in this case two analog signals. Most simply said, if one signal is greater than the other, comparator returns digital one, and if one is less than the other, it returns digital zero. And these two signals are signals from analog input pin and a signal from DAC. At the start of conversion ADC puts highes digital value to DAC and it converts it to highest analog voltage than DAC can produce (Aref), than comparator compare this value from DAC and value from analog input pin. After this ADC reads value from comparator, determining if DAC voltage is greater than or lover than voltage from analog input pin. Than ADC splits DAC value into two, and whole comparing/reading process is repeated. This process is repeated until ADC locate input voltage level in between two of 10 bit voltage levels that DAC can produce. 10 bit is the resolution of ADC (for Arduino Uno main chip = ATMega328P). The resolution of an ADC is defined by dividing voltage level on Aref pin by the total number of possible conversion values 10 bit = 1024. This means that many voltage levels in between 0V and Aref can be read by ADC as 1024 different values. If voltage reference (Aref) is 5V, the smallest voltage level that ADC can differentiate is 5 / 1024 = 0.00488 V = 4.9 mV.

ADC can read only one analog pin at the time. In order to read 6 analog pins that Arduino Uno has, reading process must be multiplexed. This means that ADC will read one and store its value, then another, and so on until the last, sixth analog input pin.

But Arduinos are created in order to reduce this complexity of adjusting all ADC registers to just one function, analogRead(). This function returns number that is 10 bit representation of analog voltage level on particular analog input pin. If we are measuring analog voltage, than you have to divide this number by 1024 and subtract it from 5. And then we will have value of actual voltage level on analog input pin, within small error range (∓4.9mV for Aref = 5V).

So, finally theory is over, let’s do something practical. Let’s create voltage divider using 10k ohm resistor and photo resistor.

What is voltage divider? Well, the name says everything it is circuit which divide voltage. We use two resistors, connect them in series, and at two ends of serial connection we connect input voltage and GND, and output is between middle part and and GND. Like on image.

Тhere is an equation that describes the dependence between the input and the output voltage:

Vout = Vin * R2 / (R1+R2)

R1 is resistance of first resistor, and R2 is resistance of second resistor. If both resistors have the same resistance, this voltage divider is actually voltage splitter. like on image in upper part.

If Vin = 9V, R1 = 10k ohm, and R2 = 1k ohm, we get Vout = 818mV like on image in the middle. If Vin = 9V but we replace R1 with R2 than we get Vout = 8,18V, like on image in lower part.

You can use equation from above to calculate this. You will get the same results.

For next example we will use Arduino Uno, 10k ohm resistor, photo resistor and few jumber wires. Connect everything like on diagram and upload to Arduino Uno this sketch: File > Examples > 01.Basics > AnalogReadSerial. Start Serial Monitor. Cover photo resistor with something from light. See the difference in readings.

Now upload this little sketch:

<p>double voltageRef = 5.0;    // Voltage reference level<br>double resolution = voltageRef / 1024.0;  //  smallest value that ADC can read = resolution
double voltageInput = 0;    // Input voltage level</p><p>void setup() {
}</p><p>void loop() {
    int sensorValue = analogRead(A0);</p><p>    //  equation for converting digital value from ADC into actual voltage level
    voltageInput = voltageRef - (resolution * sensorValue);</p><p>    Serial.println(voltageInput);

This is a sketch that converts this digital number, read from ADC, into actual representation of voltage level. If you start Serial Monitor now, you will see voltage level on analog input pin A0.

Step 9: PWM

To turn on and off is fun, but what if you want an LED to fade instead to blink? (we did this in one of previous steps). To do that, we need to figure a way to make intermediate voltages from the AVR’s digital one and zero. This is where PWM, Pulse Width Modulation, comes in handy. Simply said, PWM toggles the digital output on and off very fast, so quickly that whatever is attached to the output can’t react fully. The result is that the output sees a voltage that is proportional to the average procent the time that the AVR spends with its output on.

Like the name says, PWM is modulation of width of single pulse. For digital signal, it can have two states, digital one and digital zero. For digital voltage used in Arduino Uno, digital zero is 0V and digital one is 5V. So how is PWM connected with this? If we define one period of signal as digital one (ON state) half time, and another half is digital zero (OFF state) like on image, than this width of pulse is time duration of ON state. So in PWM we change this width or duaration of a ON state to achieve something. What is that? An LED or servo motor, sees this fast switching on/off as average of states, so that if half time is ON and half time is OFF, average is 2,5V and LED will be half as bright as on 5V. If it is 25% of period, ON state and 75% is OFF state, average is 1,25V and LED will be less bright.

Relationship between ON state and OFF state is called duty cycle, and it is represented in % of ON duration. This is an image to show this.

In our Fade sketch, we used analogWrite() function to create a PWM output.

Resolution of PWM output for Arduino Uno is 256 steps, which means that PWM output can have 256 different voltage levels, 0 = 0V, 255 = 5V, and 1 = the smallest level = 5V / 256 = 0.02V.

By changing PWM, we change duty cycle of output signal, so that an LED sees only average voltage level. In Fade sketch we changed this duty cycle by adding brightness step to brightness level on every cycle in loop function. If brightness level is less than 0 or higher than 255 we change the sign of brightness step, so that brightness level starts to fall down if it is more than 255 or rise up if it is less than 0. And this is repeated endlessly, making an LED light up and than fade, again and again.

We didn't wrote about frequency until this point because it could mess things up if we wrote everything together. We think that it is best to first understand duty cycle and resolution before learn about frequency.

What is frequency? Frequency is speed of change between ON and OFF states for digital signal. It is represented in Hertz (Hz). If we define period as length in time of one ON and one OFF state, than frequency is number of periods in one second. If we have one period in one second, this is frequency of 1Hz, and if we have 100 periods in one second, we have frequency of 100Hz.

Now everything together. PWM frequency of Arduino Uno is close to 500Hz. Signal change its ON/OFF states 500 times per second. For LED this is so fast that it can not switch on or off fully so it can only see average voltage level. If we make 100% duty cycle, LED will see 5V, and if we make 0% duty cycle LED will see 0V.

We will test this by reading analog value from photoresistor, and creating PWM with LED. LED will be light source for photoresistor. Connect everything like on diagram, and upload this sketch to your Arduino Uno. Bend the LED over photoresistors.

<p>int led = 9;</p><p>void setup() {
    pinMode(led, OUTPUT);
}</p><p>void loop() {
    analogWrite(led, 255);
    Serial.println(5.0 - double(analogRead(A0))/1024);
    analogWrite(led, 100);
    Serial.println(5.0 - double(analogRead(A0))/1024);
    analogWrite(led, 0);
    Serial.println(5.0 - double(analogRead(A0))/1024);

Start Serial Monitor and see the magic.

In this sketch everything are what we did in previous steps, and we don't need to explain it.

In next instructable we will use PWM to control 4 DC motors for Robo Car.

This is all for this Instructable. If you have any questions, feel free to ask.

You can reach us under at any time and we will be glad to help you, or visit us under