Lesson goal: Linearly scale a potentiometer

Previous: Real time light-level plot | Home | Next: Make an etch-a-sketch

In a previous lesson, you learned how to read a voltage, as seen (or sampled) by the Arduino. This lesson will show you how to read a voltage, as generated by a potentiometer, then construct a linear function of the form $y=mx+b$ to scale it to the screen.

Now you try. Scale the voltage from the potentiometer to fill the entire plot area!

Type your code here:

See your results here:

Wire up this circuit, using the potentiometer ("po-ten-tee-ah-meter"). Run the above code, and turn the potentiometer along its full range of motion. You should see the values change from 0 to 5 Volts.

So the angular position of the knob of the potentiometer can split the +5V from the Arduino into any fractional voltage, from 0 to +5V. (It is called a "voltage divider" in this circuit.)

Next, change the above code to

for x=1,100 do
v = analogread(A0)
y = v

This will plot the voltage of the potentiometer vs. the variable x, which runs from 1 to 100. But notice when you turn the knob, the graph stays kind of small; it doesn't fill the while screen. No good!

The screen goes from about -200 to 200 in the vertical dimension. So, figure out a function of the form $y=mx+b$ that will scale 0 Volts to position -200 and 5 Volts to position 200. Fill your function into the line of the code y = v. When you run the code again and turn the potentiometer, you should see the dots now fill up the screen! What a great use of a linear function! Dismiss.

Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.