Now that we have acquired the ELEGOO kits, we can start to work with the actual Arduino. For the first half of this assignment, we’ll practice utilizing ports, the serial monitor, and wiring by running the code we wrote in Assignments 1 and 2 in the actual Arduino IDE.
In order to access our Arduino, we have to use the Arduino IDE (integrated development environment) instead of TinkerCad. Visit the link below to get started.
If you’re running Windows, it is advisable to use the ZIP non-admin install to avoid any errors. If you’re running Mac or Linux, select the appropriate option from the menu as shown. Clicking any of these will take you to a page which asks for a suggested donation. You can select “Just Download” to bypass this.
From here, you can open up the downloaded folder. Select the application, and unzip when prompted. This may take a while. When it’s finished, you should be able to open the application by selecting the executable.
Once you have the IDE open, you’ll need to plug in the Arduino and select the port. You must ALWAYS do this when you plug in the Arduino, or it will give you an error (see below):
Notice how before I’ve plugged in the Arduino it shows the red text and does not change. After I plug in the Arduino and go to Tools > Port > and select the one that says “Arduino/Genuino,” I can successfully compile (indicated by the green bar in the bottom right).
Exercise 1: Serial Monitor
- Plug your arduino into the computer
- Select it from the ports list
- Copy and paste in your code from Assignment 1, Exercise 5
- Run it and observe the output in the Serial Monitor
- Try changing the baud (bottom right corner of the Serial monitor). What happens? Copy and paste an example output when the baud is changed. Why do you get this output?
Exercise 1: Output
Exercise 2: LED Blinking
Now, we want to wire up the RGB LED and reproduce Exercise 7 from Assignment 2.
- Connect GND to the blue (-) column on your breadboard
- Wire the RGB LED as shown below (use male-male jumper cables)
- Be sure to include the 100Ω resistor to avoid damaging your LED
- Run your code from Exercise 7.
Exercise 2: Output
Part 2: Analog Input
For this section, we’ll start working with the sensors that we’ll employ for Project 2. These are photoresistors, and we measure the change in resistivity across them to measure ambient light. Because these sensors are light-based, they are extremely responsive and useful for high-speed applications. Like any resistor, the leads have no polarity, meaning that we can wire it either way and there will be no difference (unlike the LED, which has a right and wrong direction). See the diagram below for wiring of the photoresistor:
The way this works is we run low power (hence the 10KΩ resistor) through the photoresistor, and we have a wire as a probe connected to the A0 input pin. Although the digital pins 1-13 can also handle analog, they can only reliably handle analog output (not input), and it’s best to use the PWM (~) pins for that. When doing analog input, we wire to the A pins. When declaring the pins, we declare them as input, but we don’t need to specify that it is an A pin. For example, we can simply declare pin 0 as input, and the Arduino will know that when we call the analogRead function, it should read from the A pins.
Exercise 3: Photoresistor
- Wire up the photoresistor as shown
- Declare pin 0 as an input
- Use analogRead to read the value of the photoresistor
- Print out that value to the Serial monitor
Exercise 3: Output
You should see a readout in the range of about 800-900, but this value may vary depending on the ambient lighting of your environment. Try waving your hand over the sensor. As the light read by the sensor is reduced, the readout should dip to a lower number.
Before attempting the next exercise, be sure to check out the following introduction to "if" conditional statements in arduino.
Exercise 4: Photoresistor Threshold
For this exercise, you should recreate in class exercise #1 on your own. Wire up two photoresistors and two LEDs as shown below.
Use your code from exercise 3 to get readout from both photoresistors. Once you have successfully printed the readouts of the photoresistors to the serial monitor, take note of the highest and lowest readouts depending on the lighting. Keeping track of the readouts will give you an idea of the range of the thresholds. The threshold is the value you decide where you can safely say that the photoresistor has gone from covered to uncovered.
Using the difference between threshold 1 and threshold 2 (threshold1 - threshold2), create four conditions that will turn both LEDs on if the photoresistor values are within 50 units of each other and above the bright threshold, will turn the left LED on if threshold1 is greater than 50 units of threshold 2, turn on the right LED if threshold2 is greater than 50 units of threshold1, and turn both LEDs off if they are within 50 units of one another and below the brightness threshold.
Exercise 4: Output
- Try changing the baud (bottom right corner of the Serial monitor). What happens?
- What is the difference between these two buttons? What does each one do?
Submitting the Assignment
For each of your codes, take screenshots of the IDE, Serial monitor, and your Arduino/breadboard. Compile these into a Word document using the template we provide, and submit as a PDF with your public project link.