Arduino Worksheet 5 (3 Parts)
Link to MEGN 200 Arduino E-Textbook
Light Locator Circuit
In this activity, we’ll learn how to use a stepper motor to move by a precise number of steps.
Then, we’ll return to the photoresistor: the resistive-based light sensor we used during AW1 to
create a night light and alarm clock. We’ll then combine both the stepper motor and the
photoresistor to make a 1) control system that scans an area to point to the brightest light source
in the scanned area and 2) then can follow the light source.
1. Stepper Motor and Go To Angle Function
To start this activity, we will wire the stepper motor in your kit and test its functionality. A
stepper motor is a brushless DC motor whose full rotation is divided into an equal number of
discrete intervals (called steps). To get your stepper motor working, follow Lesson 23 in the E-
Textbook and use the provided code there (“stepper_oneRevolution.ino”) to get the stepper
motor circuit working and understand its basic functionality.
NOTE: Do NOT change the value of “const int stepsPerRevolution = 2048;” to get your motor
to take a certain number of desired steps. The variable stepsPerRevolution is used to initialize the
stepper motor in setup and changing this will cause your motor to work improperly. If you want
to drive the motor a certain number of steps, change the input to the “step” function directly.
Next, you’ll want to follow Lesson 24 in the E-Textbook and open the starter code
“CNC_jog_stepper_starter.ino”. You’ll need to write a custom stepper function called
stepperGoTo that that makes the stepper move from its current position to the new desired
positon. Don’t forget:
• The stepper doesn’t know or care about where it is or where it’s going
• It only cares about two things: # of steps and which direction
You do not need to make any edits to the setup and loop sections of the starter code, only
write and call the stepperGoTo function. Again, the goal is to write the function so that the
stepper motor can move from the current position to a new position given by the joystick. The x-
axis provides direction and angle, and the joystick button should move the stepper to the home
(0) position.
Question 1.1: What is one line of Arduino code that would make the stepper motor move
counter-clockwise half a revolution in your code?
Answer:
Question 1.2: What is one line of Arduino code that would make the stepper motor move at 5
RPM each time it ran?
Answer:
Question 1.3: We have used three motors now – the DC motor, the servo motor, and the stepper
motor. Describe the basic functionality of each motor and pros/cons of using each of these three
motors.
Answer:
Question 1.4: Copy and paste the stepperGoTo function code that you wrote below.
Code:
2. Light Locator!
The goal of this part is to write a code that starts with a button press to cause the stepper motor
to rotate 180 degrees in single degree increments (note that 2048 steps for this stepper motor is
360 degrees, or about 6 integer steps per degree), record the step position and angle of the
brightest light source during this 180 degree sweep, rotate back to the position of the brightest
light source and pause for 1 second, then rotate back to the starting position of the stepper motor.
This functionality is similar to what a solar tracker for a solar panel does.
There is no starter code for this part - we expect you to develop the code from scratch
starting with the same “int stepperGoTo(int newAngle, int currentAngle)” function you
developed in Part 1. Please make sure to watch the Demonstration Video for further info about
how the light locator system works: ([Link] It’s
best to test your code while everything is attached to the breadboard first to verify the code
functionality. Also, note that you can either use a push button or the joystick as a button.
1.) First, you’ll need to get the stepper motor to rotate 180 degrees and then back to its
starting position of 0 degrees. Then, you can add a push button to trigger the stepper motor to
rotate 180 degrees and then back to 0 degrees (the starting position). Before you press the button
and after the stepper finishes its sweep, the stepper should remain stationary at 0 degrees until
you press the button again. Make sure to use the function you developed in Part 1.
2.) Next, you’ll need to set up the photoresistor like you did in AW1. You’ll need to keep track of
the photoresistor values along with the current step and the calculated angle during the stepper
motor’s sweep. During the 180 degree sweep from the starting position, you need to keep track
of the highest reading received from the photoresistor voltage divider input at A0, along with the
associated stepper angle. A good way to accomplish this is to use an “if” statement that will keep
updating the brightest light value current angle if the current light reading is greater than the
previous brightest light value reading. To start, you can shine a bright light from a cell
phone/flashlight as the stepper rotates and keep track of the correct position (drawing a circle on
a piece of paper and taping the stepper motor to the center is a good option).
3.) Once your code is able to identify the brightest light value, step position, and calculated
angle, implement the ability for the stepper motor to rotate back towards the angle position
corresponding to the brightest light after it completes its sweep, pause at that location for at least
1 second, and then rotate back to the starting position.
4.) Finally, you will need to attach the photoresistor to the stepper motor. You may use the
provided custom plexiglass mount for the photoresistor, or you may create your own system
using cardboard and tape. Thread the leads of the photoresistor through the holes on the mount,
then “extend” the photoresistor leads using the male to female DuPont jumper cables from the
kit. The mount slips onto the motor shaft. Pay attention to cable management so the motor can
spin without pulling excessively on the wires. See the photos below for an example.
Example Design of a Photoresistor Light Scanner
The plexiglass support can be used Weave the photoresistor leads Connect the photoresistor to the
to hold the photoresistor to the through the holes in the plexiglass circuit using male to female
stepper motor support jumper cables
Close up of the photocell securely set in the plastic stepper motor pointer
Some general tips for debugging:
- Make sure your hardware is working before you start working through the logic and
software associated with the brightest light tracker. Make a quick script that spins the
stepper motor and one that reads the value of the photocell and prints it to the Serial
Monitor. This will help you understand if a bug is a hardware or software issue. You may
want to repeat this check when you switch to the plexiglass or cardboard-mounted
photocell so you know it’s still working right.
- Use the Serial Monitor to help you debug your code. You can print sensor values as they
change, but you can also print strings to help you understand where you are in your code
when something is going wrong. For example, a print of “If Statement A” in one of your
conditional statements can help you understand if your code is getting to that conditional
statement or if it is being skipped over.
- Try to break each problem you face into smaller pieces, and rule out what the issue might
be systematically with targeted testing or printing.
Once the code and circuit are working, run the code 3 times, each time shining a light at
different locations along the scan. Remember to press the button before each scan to start it and
after the last scan was completed. Make sure to record all three sweeps in your video.
Introduce yourself as usual, and explain what is happening in your circuit and code as you
show all three sweeps.
Question 2.1: Write down the angles (in degrees) and the brightness values found by your solar-
sweeper in the table below. You can print out the final brightness and angle values using the
Serial Monitor and [Link]() statements in the code.
Fill in the table:
Reading 1 2 3
Angle Location (o)
Brightness Value
Question 2.2: Submit a video of your 3 scans working while narrating all of the features of the
code and system (3-minute max). Move the light between trials. Introduce yourself as usual.
• First, show your face
• Then, show the working circuit and code
• Finally, while demonstrating and showing your working code and circuit, discuss how it
works.
Video Link:
3. Arduino Final Boss: Light TRACKER!
On this part you are permitted to use Generative AI to assist with the designing of your Light
Tracker. You will be asked to provide and refelct on your use of, or non-use of, Generative
AI.
Start with your physical set-up from Part 3 in your light locator and add a second photo sensor,
so you have two right next to each other (side by side on the plastic pointer).
Before you get started check out THIS VIDEO DEMO of a functioning light locator and tracker.
Your goal is to add the ability to not only locate the light where it is the brightest (like you did in
Part 3), but also to follow (or track) the light. You’ll be building off of your code from part 2,
Now you’ll be using two photoresistors to locate the brightest points within a 120 degree
sweep, tracking that brightest point for 10 seconds, and then finally returning to home and
awaiting another button press.
To prepare for this final AW Boss, let’s start by writing some pseudocode to breakdown our
problem and first think through the necessary programming logic to complete this design
task.
- You may want to think about
o How to use two photoresistors to:
▪ 1) Identify the brightest point
▪ 2) Track the bright point by figuring out which direction it is moving
o You may need toplay around with the spacing of your photo cells and how
forward/outward they face.
o Also consider:
▪ What are the values of each photocell at the brightest point? Ideally they are
the same but perhaps they aren’t. How could you alter their readings before
tracking so that the brightest point is essentially a baseline or datum of where
to track from?
▪ If you are having trouble getting both photocells on the plastic arrow
considering designing a different mount (adding a previous design below for
some inspiration). Either way you’ll need some tape and real engineering
design here.
- The photocells should always be pointing to the light source, no matter where you move it in
the 120 degree range of motion.
- How can you perform this track function for 10 seconds and then return home? Using
functions that halt your code could be DEADLY (for your grade) as the light source could
move but your Arduino won’t notice and track. Think. THINK!
Question 3.1: Plan out your light tracker code before diving in. Write pseudocode outlining
the logic required–and that you’ll be writing-to complete this task.
Paste that pseudocode below:
Good luck, we're all counting on you.
Question 3.2: Submit a video of your Arduino locating and tracking a light source. Introduce
yourself and do the usual within the video.
• First, show your face
• Then, show the working circuit and code
• Finally, while demonstrating and showing your working code and circuit, discuss how it
works.
Video Link:
Gen AI:
Please respond to the following questions below.
Question 3.3a: Did you use generative AI during any part of this Sprint?
• If not, please elaborate on why you did not use it/feel the need to use it. Consider how it
may have assisted during this Final Design Sprint and provide some information on how
you may use it in the future.
• If you did use GenAI, please brifely respond to the prompts below:
What GenAI tool did you use?
Why did you use this tool?
What information did you have to provide GenAI in order for it to return what you
were looking for?
GenAI can only regurgitate existing information. What source(s) do you think it
used to compile its responses to your prompts?
How did you ensure the accuracy and reliability of the information provided by
the GenAI tool?
How did using GenAI alter your ability to think critically, solve problems, and
complete the Sprint independently?
(please copy and paste all interactions with GenAI below in the GenAI Appendix
provided)
Question 3.3b: Paste all interactions between any and all generative AI tools are provided below for
documentation of the tool and it’s use cases:
4. Debugging Practice AW5!
NO DEBUGGING PRACTICE THIS WEEK. Woot WOOT! Definitely make sure you
know how to write functions and how to control a stepper motor vs. a servo.
AW5 RLA
RLAs are Real Life Assignments where we ask that you take a moment to consider the things
you learned in class, whether they be theory or hardware or software or WHATEVER, and
where you find them/how they could be used to solve problems in your daily life.
Question 3.4: For AW5’s RLA, please respond to the prompt below: How confident do you feel
about your programming abilities now compared to when you started? What are the most
important concepts or skills you learned in programming? Can you describe a situation or design
project, outside of this class, where you could/would/have/will apply what you’ve learned about
programming?