loading

The following information is a single lesson in a larger project. Find more great projects here.

Return to Previous Lesson: Using Sensor Input and Serial Output

Lesson Overview:

Now we'll learn how to create "analog" signals!

Step 1: Getting Started

The Arduino is capable of directly reading analog signals whose voltage is somewhere between 0V and 5V, like those produced by light sensors and temperature sensors. This can be used to determine how much light is falling on a light sensor, for example.

The Arduino is also able to drive digital output signals, where the voltage on a pin is either 0V or 5V. However, it is not able to directly drive voltages in-between.

Through the use of some simple circuitry and what is known as pulse width modulation, you can easily get past this limitation.

  1. Continue to the next step.

Step 2: Pulse Width Modulation

An oscillating digital signal is alternately driven high and low in a repeating pattern. Each high to low to high period of time is called a cycle.

Pulse width modulation (PWM) creates an oscillating digital signal, where the amount of time that the signal is high and the amount of time it is low within a cycle is not necessarily the same. It can be controlled such that the width of the high pulse and the width of the low pulse can vary.

  1. Continue to the next step.

Step 3: Instructing the Arduino

You can tell the Arduino to produce a PWM output signal using the "analogWrite" function.

Perhaps you are wondering why the function is called "analogWrite" when it controls a digital signal. This will be revealed as the lesson continues.

PWM only works on a few of the digital output pins. For the Arduino Uno, PWM can be produced on pins 9, 10, and 11.

Modify the code to set pin 11 as an output, driving a PWM signal.

  1. Press "Code Editor" to open the code editing pane.
  2. Add the following line to the declarations section (line 4): int pwmPin = 11;
  3. Add the following line to the end of the "setup" function: pinMode(pwmPin, OUTPUT);
  4. Add the following line to the beginning of the "loop" function: analogWrite(pwmPin, 50);
  5. Press "Code Editor" again to close the code editing pane.
  6. Continue to the next step.

Step 4: Trying It Out

Before simulating this, you will need to have some way to observe what's going on. Add an oscilloscope to monitor the PWM signal.

  1. Press "Components" to open the components selector,
  2. Find the oscilloscope and add one as in the picture.
  3. Connect the left terminal to the blue row at the bottom of the breadboard (GND).
  4. Connect the right terminal to digital pin 11 of the Arduino.
  5. Press "Start Simulation".
  6. Click on the oscilloscope and change the "Volts per division" to 2 V, the "Time per division" to 2 ms, and uncheck "Autoscale"
  7. Continue to the next step.

Step 5: Pulse Width

You should see an oscillating digital signal on the oscilloscope like the picture below. Notice that for each cycle, the width of the high pulse is less than the width of the low pulse.

This is where the "pulse width" part of the term "pulse width modulation" comes from.

The second argument of the "analogWrite" function can accept any integer value between 0 and 255. What happens to the picture on the oscilloscope when you change the value in the program.

  1. Press "Stop Simulation".
  2. Change the value in the "analogWrite" instruction to different values and simulate again.
  3. When you are done, press "Stop Simulation".
  4. Continue to the next step.

Step 6: Controlling the Width

You may have observed that the width of the high pulse is directly controlled by the "analogWrite" argument. When the argument is small, the width is narrow. When the argument is large, the width is wide.

For a value of 0, the high pulse has no width at all, and disappears entirely. The output is an unchanging 0 V.

When the argument is 255, the high pulse becomes so wide that the low pulse disappears. The output becomes an unchanging 5 V.

  1. Continue to the next step.

Step 7: Adding a Control Knob

You can add a control knob to dynamically change the width during simulation. You will connect a potentiometer to one of the analog input pins of the Arduino to control the pulse width.

The analog inputs are read using the function "analogRead". This function instructs the Arduino to measure the analog signal and convert it to a value between 0 and 1023.

You can then convert this value to the right range to control the pulse width by dividing it by 4.

  1. Add a potentiometer to the breadboard as in the picture.
  2. Connect the left terminal of the potentiometer to the blue row at the bottom of the breadboard (GND) with a wire.
  3. Connect the right terminal of the potentiometer to the red row at the bottom of the breadboard (+5V) with a wire.
  4. Connect the center terminal of the potentiometer to analog input pin A0 of the Arduino with a wire.
  5. Open the code editor and add the following line to the declarations section: int pwm = 0;
  6. Replace the instructions in the "loop" function with: pwm = analogRead(A0) / 4; analogWrite(pwmPin, pwm); delay(500);
  7. Press "Code Editor" to hide the code editing pane.
  8. Press "Start Simulation".
  9. Adjust the knob of the potentiometer to see what happens.
  10. Press "Stop Simulation".
  11. Continue to the next step.

Step 8: Digital to Analog

You still only have a digital PWM signal. How can this be turned into something analog?

By adding a single resistor and capacitor in series between the PWM signal and GND, you can create what is known as a low-pass filter.

The low-pass filter takes a quickly changing signal (like the PWM output), and averages the voltage over time.

When the high pulse is narrow, it doesn't contribute much, and the average voltage will be fairly low.

When the high pulse is wider, it contributes more and the average voltage becomes larger.

  1. Add a resistor and a capacitor to the breadboard.
  2. Connect a wire from digital pin 11 to one side of the resistor.
  3. Connect the other side of the resistor to the capacitor by either moving the capacitor, or adding a wire.
  4. Connect the other side of the capacitor to the blue row at the bottom of the breadboard (GND) with a wire.
  5. Copy and paste the oscilloscope and place it to the right of the one that is already there.
  6. Connect the left terminal of the new oscilloscope to the blue row at the bottom of the breadboard (GND) with a wire.
  7. Connect the right terminal of the new oscilloscope to the side of the capacitor that is connected to the resistor.
  8. Simulate the circuit and observe what happens when you adjust the knob.
  9. Continue to the next step.

Step 9: Low Pass Filter

You may have noticed that the oscillating signals in the two oscilloscopes look almost the same. However, the signal in the oscilloscope on the right has slightly curved edges.

This is because the new part of the circuit is averaging the voltage, but it is doing it too quickly, so you can only see its effect right at the high/low transitions.

Increase the value of the resistor to see what happens.

  1. Press "Start Simulation" and adjust the knob so that pulses are visible.
  2. Change the resistor value to 10 KΩ, 100 KΩ, and 1 MΩ to see what happens.
  3. Press "Stop simulation"
  4. Continue to the next step.

Step 10: Measuring the Result

Increasing either the resistor or capacitor values (or both) effectively increases the amount of time that the low-pass filter is averaging the signal.

With the resistor value at 1 MΩ, the averaging becomes pretty good and the result is that the cycles of the PWM have been converted to an average analog voltage level by the low-pass filter.

Add a multimeter to the circuit to read the analog voltage.

  1. Click "Components" to open the components selector.
  2. Search for the "Multimeter" and add it to the right of the oscilloscopes.
  3. Click "Components" again to hide the components selector.
  4. Connect the left terminal of the multimeter to the blue row at the bottom of the breadboard (GND) with a wire.
  5. Connect the right terminal to the side of the capacitor that is connected to the resistor. (same as the right oscilloscope).
  6. Start the simulation and observe the multimeter as you adjust the knob.
  7. Continue to the next step.

Step 11: Finishing Up

You now know how to instruct the Arduino to create a pulse width modulation signal, and how to convert that signal to an analog voltage.

Congratulations, you have completed this project!

Check out other great projects here.

About This Instructable

140views

7favorites

License:

More by circuits:Move Circuit Assembly Glow Circuit Assembly Introduction 
Add instructable to: