Arduino Lesson #2

Lesson Objectives: Understand how to interpret resistor colors, to properly connect wires on a breadboard, to draw a simple circuit diagram, and then to demonstrate Ohm’s Law using an LED, resistors, and a battery, and then an Arduino Nano.

Materials required:

  • 4-5 resistors of various resistance. Example uses 100, 220, 1k, 10k, and 1M Ohm resistors.
  • Breadboard
  • Wires
  • LED
  • 9 V battery
  • Microprocessor with cable to attach to computer

Here is sample code you can use for a simple blink program:

/*
  This code is a modification of the code found here: 
  https://create.arduino.cc/projecthub/Ayeon0122/reading-a-photoresistor-1e705e
 
  Read a photoresistor (light sensor) to detect "darkness" and turn on an LED when it
  is "dark" and turn back off again when it is "bright. Print analog read to serial monitor. 
 
  Code was modified to allow viewer to see the value read at the voltage divider. 
  The code was also modified to include an output format that is more easily exported as a .csv.
  AnalogRead values are sent to the serial monitor so values can be analyzed.
*/
 
 
// As usual, we'll create constants to name the pins we're using.
// This will make it easier to follow the code below.
 
const int sensorPin = 0;
const int ledPin = 3;
 
// We'll also set up some global variables for the light level a calibration value and
//and a raw light value
int lightCal;
int lightVal;
 
int readingNumber = 0; //sets the first reading value to zero, so all future values are time in seconds from start
 
void setup()
{
  // We'll set up the LED pin to be an output.
  pinMode(ledPin, OUTPUT);
  lightCal = analogRead(sensorPin);
  //we will take a single reading from the light sensor and store it in the lightCal
  //variable. This will give us a prelinary value to compare against in the loop
 
  Serial.begin(9600); //initializes serial port with baud rate of 9600, pretends the USB is an old school serial port
  //baud rate is a way of confirming that the send rate equals the receive rate
  Serial.println("Serial is initialized"); // verify that it does something, starts text on a new line after function
  Serial.print("Initial light reading: "); //verify the initial reading, appends to previous line, quotes are not values but actual words to be included
  Serial.println(lightCal); //adds the lightCal reading after the previous colon
  Serial.println("ReadingNumber , LightValue"); //Headers for the data that will be collected, CSV
  
}
 
 
void loop()
{
  //Take a reading using analogRead() on sensor pin and store it in lightVal
  lightVal = analogRead(sensorPin);
  Serial.print(readingNumber); //prints the reading number to the serial monitor
  Serial.print(" , "); //prints a comma in the serial monitor to aid in CSV data collection
  Serial.println(lightVal); //prints the current photoresistor values
  readingNumber++; //shorthand of saying new variable = previous variable +1
  delay(1000); //sets data collection sampling rate to 1 second, remember time is in thousandths of a second
 
  //if lightVal is less than our initial reading (lightCal) minus 50 it is dark and
  //turn pin 9 HIGH. The (-50) part of the statement sets the sensitivity. The smaller
  //the number the more sensitive the circuit will be to variances in light.You may need to adjust this value
  //depending on environmental conditions, the amount of light in your room initially. 
  if (lightVal < lightCal - 50)
  {
    digitalWrite(3, HIGH);
  }
 
  //else, it is bright, turn pin 3 LOW
  else
  {
    digitalWrite(3, LOW);
  }
 
}

Other helpful links that will assist with learning the content in this video:

  • Make sure you bookmark a good website or include an image in your notebook to help you determine the size of any resistors. This website is one of my favorites followed shortly by this website.
  • If you are unable to determine the resistance through the color codes, you can always use a voltmeter. Simply turn the voltmeter to the symbol for resistance, Ohms (Ω). Then place one lead on the metal wire on each side of the resistor. The screen will identify the resistance.
  • If you are interested in getting a head start on symbols you might see in circuit diagrams, links like this will introduce you to commonly used symbols.
  • This website breaks down the steps of this lesson in even more detail.

In your Arduino Notebook: Make sure you include a circuit diagram, the values of your resistors, a description of positive and negative terminals of LEDs, an explanation of resistor codes, the code you used with what you changed and why, Ohm’s Law and how it applies to the circuit, and a picture of your breadboard setup.