1 of 39

Unit 1 - Training�Lab 1C : Introduction to Multimeter and Arduino code

UCLA Physics Department

University of California, Los Angeles

Department of Physics and Astronomy

Physics 4AL

2 of 39

Outline of Lab 1C

  • 1C In-Lab
    • Intro to Digital Multimeter.
    • Intro to Arduino code.
    • Intro to Circuit components.

UCLA Physics Department

3 of 39

Introduction to Digital Multimeter

UCLA Physics Department

4 of 39

Digital Multimeter

5 of 39

Potential measurements with Digital Multimeter

DC Voltage

AC Voltage

Resistance

Continuity

Capacitance

Frequency

Current (smallest)

Current (medium)

Current (largest)

Use left input

6 of 39

Measuring resistance with a multimeter

Set the multimeter to the 𝛀 symbol. Connect the two leads to the two ends of a 100 Ω resistor without connecting to any power source. The multimeter might take a moment to settle on a value, but it will read out a value close to 100 ohms.

7 of 39

Resistance measurements (Checkpoint 1)

UCLA Physics Department

  • Choose your own resistors to measure in Ohms. You can also measure the resistance of two resistors in series or parallel. Fill out this table by measuring three resistances.

Stated Resistance (Ω)

Measured resistance (Ω)

8 of 39

Introduction to Arduino code

UCLA Physics Department

9 of 39

Arduino Input/Output

Input/output pins

PWM Pins(~): 3, 5, 6, 9, 10, 11

Digital Pins: 2, 4, 7, 8, 12, 13

USB input/output/power

Battery power

Output power + ground

Analog Input

10 of 39

Connecting an Arduino

  • Connect one end of blue cable to Arduino and other end to computer.
  • Check if small lights are turned on.

UCLA Physics Department

10

11 of 39

Arduino Connection

UCLA Physics Department

12 of 39

Code basics

  • We will be writing programs.
    • List of commands that are stored in a file.
  • Those commands are uploaded into the Arduino.
  • The Arduino reads the commands and executes them.

UCLA Physics Department

13 of 39

Code basics - void setup()

UCLA Physics Department

UCLA Physics Department

  • We begin our code with

void setup() {

Commands here

}

  • These are the commands that the Arduino will run first.
  • The list of the commands is the body of the function.

14 of 39

Code basics - void loop()

UCLA Physics Department

  • After the setup function we have a loop function.

void loop() {

Commands here

}

  • This runs after the setup function and the commands in the body of this function will repeat over and over unless instructed to stop.

15 of 39

Code Basics - Variables

  • Before the code we can declare variables.
  • Variables in code work like variables in algebra.
  • Variables are case sensitive and cannot have spaces.
  • The code will then use that variable name in all of the functions.

UCLA Physics Department

16 of 39

Code Basics - Variable types

  • Structure
    • [type of variable] [name of variable] = [value];
    • int ledPin = 13;
  • All variable declarations need to end with a semicolon.
  • Int stands for integer, a number with no decimals while float stands for a floating point number.
    • int: 20
    • float: 20.40

UCLA Physics Department

17 of 39

Code Basics - Digital Input/Output

UCLA Physics Department

  • pinMode(pin,mode);
    • Sets the mode of the pin # to either INPUT or OUTPUT.
    • Is the pin receiving or issuing signals.
  • digitalRead(pin);
    • If pinMode is set to INPUT, the Arduino will tell you if the state of the pin is HIGH or LOW.
  • digitalWrite(pin,value);
    • If pinMode is set to OUTPUT then this can write a value of HIGH(5 V) or LOW(0 V).

18 of 39

Example code - Comments

  • Comments begin with //.
  • These are intended for the human reader to understand what the code does.
  • When the code is compiled, these comments are not read by the computer.

UCLA Physics Department

19 of 39

Example code - Upload

  • After the code is written click checkmark to compile the code. This checks to the code to make sure it’s ready to run on the Arduino and tells you if you have any errors that you need to fix.
  • Click the right arrow to upload the code to the board, telling the Arduino to run the program.

UCLA Physics Department

20 of 39

Testing Arduino with multimeter

  • Use your multimeter and change the dial to V (with solid and dashed lines) to measure DC voltage. This is to utilize your multimeter as a voltmeter.

UCLA Physics Department

21 of 39

Testing Arduino with multimeter

  • Put the red lead to the location you want to measure like the 5V pin on Arduino (+ side). You can use a wire with one end in the 5V pin and the other end connected to the red lead.
  • Connect the black lead to Ground (GND) pin using another wire.
  • The multimeter will measure the voltage relative to ground.

UCLA Physics Department

22 of 39

Digital Voltage Measurements (Checkpoint 2)

Measurement

Voltage (V)

5 V Pin (instructions in slide 21)

3.3 V Pin (this pin is close to the 5V pin)

Digital HIGH Pin (which pin is this in your uploaded code?)

  • Change the location of the red wire and find the voltage readings for the following.

23 of 39

Introduction to circuit components

UCLA Physics Department

24 of 39

Electrical Components

  • Resistor

  • Capacitor

  • Inductor

  • Diode (LED)

Name of Component Electrical Diagram Actual Component Arduino Schematics

25 of 39

Component Polarity

Polarized

Non-Polarized

26 of 39

Breadboard

  • Divided down the middle.
  • Power rails on both sides (shown by blue and red columns).
  • Rows are connected (shown by black lines).
  • Columns are not connected.

Black bars denote which holes are connected.

All pins on red column are connected. Similarly all pins on blue column are connected.

27 of 39

Breadboard Layout Example

  • What do you think will happen if the LED is flipped? What if the resistor is flipped?

28 of 39

LED operation

  • Disconnect your Arduino from your laptop.
  • Connect pin 13 to the longer lead of the LED through a 220 ohm resistor.
  • Connect the shorter end of the LED to the GND pin.
  • Connect your Arduino to your laptop again.
  • Take a picture of your setup with the LED glowing to submit for your post-lab assignment.

29 of 39

Commands: timing

  • delay(t);
    • Pauses for t # of milliseconds (unsigned long variable type)
  • delayMicroseconds(t);
    • Paused for t# of microseconds (unsigned int variable type)
  • time = micros();
    • Returns the time in microseconds since the program start (unsigned int)
  • time = millis();
    • Returns the time in milliseconds

29

30 of 39

External LED Blink (Checkpoint 3)

  • Type the code provided on the right into the arduino IDE.
  • Begin by declaring variables.
  • The LED pin is set to OUTPUT in void setup() allowing us to control it.
  • The LED is turned ON for 1 second and then turned OFF for 1 second. Since this is in void loop(), this runs continuously until the arduino is disconnected from power.
  • The arduino must be plugged in to your computer. Select the correct port in the IDE and compile and upload your code.
  • Complete and show the blinking external LED to your TA. Take a video of your setup and upload it to your lab google drive.

30

31 of 39

Code basics : Analog Input/Output

UCLA Physics Department

  • analogWrite(pin, value);
    • Can only be used by pins with ‘~’ preceding the pin number.
    • Int value between 0 and 255
  • analogRead(pin);
    • Used for reading analog pin values

32 of 39

LED Brightness Changing

  • This code includes an if statement
  • Structured if (condition) { commands }
  • if (brightness <= 0 || brightness >= 255) {

fadeAmount = -fadeAmount;

}

  • If the condition is not met, then the code will keep running, skipping the commands. This forces the LED brightness to increase if it hits a value of 0 and the LED brightness to decrease if it hits a value of 255 (which is the maximum).

Note: The LED pin is changed to pin number 9. Only the ponds with the squiggly line (~) in front of them are capable of analog control.

You do not have to type in the code, you can download it here

33 of 39

LED Brightness Changing (Checkpoint 4)

  • This code includes an if statement
  • if (brightness <= 0 || brightness >= 255)
  • This is the condition the statement will check to see if the brightness is <= to 0 or if the brightness is >= 255
  • The or is indicated by the rails ||
  • Conditions that can be checked:
    • Variable == value
    • Variable >= value
    • Variable < value
    • Variable != value
  • Show the completed working setup to your TA.
  • Take a video of your setup and upload it to your lab google drive.

34 of 39

Troubleshooting

  • Did you select the right Port on your Arduino IDE?
  • Do you have brackets {} around all of your functions and loops?
  • Are all of your variables defined?
  • Do all of your commands in end a semicolon?
  • Do the conditions on your loops make sense?
  • Is the Arduino plugged into your computer?
  • Are the input, power (5V) and ground (GND) pins plugged in?
  • Do you have complete circuits?

This slide will be useful throughout the quarter.

UCLA Physics Department

35 of 39

Downloads and references

  • Arduino IDE download
    • https://www.arduino.cc/en/Main/Software
  • Arduino Reference
    • https://www.arduino.cc/reference/en/
    • Contains all the references for functions and variables

UCLA Physics Department

36 of 39

Post-lab Week 2 requirements from Lab 1C

UCLA Physics Department

36

37 of 39

Resistance measurements

UCLA Physics Department

  • Submit the completed table on Gradescope.

Stated Resistance (Ω)

Measured resistance (Ω)

38 of 39

Digital Voltage Measurements

38

Measurement

Voltage (V)

5 V Pin

3.3 V Pin

Digital HIGH Pin

  • Submit the completed table on Gradescope.

39 of 39

External LED blink and brightness

39

  • Upload videos of your LED blink and LED brightness change setups into your lab google drive.
  • You will have to link the videos in Post-lab Week 2.