1 of 27

Unit 1 - Training�Lab 1A : Introduction to Python and Arduino

UCLA Physics Department

University of California, Los Angeles

Department of Physics and Astronomy

Physics 4AL

2 of 27

Class Learning

  • Use microcontrollers (Arduino) and sensors to record data.
  • Use computational and statistical methods (in Python) to analyze collected data.
  • Evaluate experimental results by comparing with predictions.
  • Investigate principles of mechanics and oscillations.
  • Work as a team to design and conduct your own creative project.
  • Communicate scientific results with reports and presentations.
  • Have fun while learning!

3 of 27

Class Learning Outcomes

  • Form a collaboration and work together
    • Design and conduct an experiment together.
    • Write reports together.
  • Perform the experiment from Hypothesis to Conclusion.
    • Define a “testable” scientific hypothesis.
    • Design the experiment setup and protocols.
    • Analyze the data systematically.
    • Evaluate the hypothesis by a quantitative analysis.

UCLA Physics Department

4 of 27

Class Schedule

  • Unit 1: Training
    • 5 Labs (two and a half weeks)
  • Unit 2: Motion
    • 4 Labs (two weeks)
  • Unit 3: Oscillations
    • 4 Labs (two weeks)
  • Unit 4: Project
    • 4 working labs and 2 presentation labs (four weeks)

Detailed schedule: https://www.uclaphysics4labs.org/weekly-schedule.html

UCLA Physics Department

5 of 27

Outline of Lab 1A

  • 1A In-Lab
    • Logistics.
    • Intro to Python.
    • Intro to Arduino.

UCLA Physics Department

6 of 27

Logistics

UCLA Physics Department

7 of 27

Lab Resources and Office hours

  • Homepage: https://www.uclaphysics4labs.org/
    • All class content will be posted here.
    • There are also extra resources and links so please look around.

  • Some of the office hours will be held in the lab room while some of the office hours will be held virtually. The virtual office hours will predominantly be used to help with coding issues.

UCLA Physics Department

8 of 27

Online Lab Resources and Format

  • Gradescope
    • Place to hand in assignments and reports as PDF for grading.
    • Join Gradescope for your lab section. Your TA will provide you the entry code.
    • Pre-lab and post-lab assignment templates are on the lab website. Make a duplicate of the slides (or of the Python notebook), meet the requirements, download as a PDF and submit on Gradescope.

UCLA Physics Department

9 of 27

Assignments

  • There are two kinds of assignments worth 30% if submission is required.
    • Pre-labs that are due 2 hours before the first lab session of each week.
    • Post-labs that are due on Sundays after the second lab session of each week at 11 59 pm.
  • A team report is due at the end Units 2 and 3 (10%+15%).
  • The Final Project includes a Presentation (15%) and Written Report (15%).
  • All Assignments and reports will be submitted as a PDF to Gradescope.
  • Attendance is worth 12% and pre-lab survey is worth 3%.
  • All of the above sum to 100%.

Detailed information about grading can be found at https://www.uclaphysics4labs.org/4al-grading-policy.html

UCLA Physics Department

10 of 27

Google Drive

  • In order to help you debug code and for groups to share data, please make a google drive folder.
    • Put it in your Lab’s section
    • https://www.uclaphysics4labs.org/google-drive1.html
    • Create a folder for your table and within that a folder for yourself with your last name, first name in your Group folder.

UCLA Physics Department

11 of 27

Introduction to Python

UCLA Physics Department

12 of 27

Python

  • Analysis on experimental data will be done via Python.
  • Python is an open source programing language that is designed to be simple to write and understand.
  • Google Collaboratory
      • Runs code on google’s servers through a web browser.
      • Good for sharing code with your lab mates or TA to debug.

12

13 of 27

Python Notebooks

  • Python Notebooks are a quick way to run immediate command line code.
  • They compile cells individually and are good for quick analysis
    • This format will be used for all the analysis in this class except maybe the final project depending on your project.

13

14 of 27

Python Notebooks

14

Google Collaboratory Notebook

Input Code

Output

15 of 27

TA checkpoint 1 - Colab Notebook

  • Follow the instructions here to create your first Colab Notebook.
  • The print() command is used to print text (or values). The text that we want to print should be within single or double quotes.

  • Within your notebook print your name and whether or not you have your Arduino yet. Use this introductory video for help.

15

16 of 27

Numbers

  • We can also perform basic calculations like addition, multiplication etc.
  • To print numerical values we do not use single or double quotes.
  • Notice that we can print without using the print() statement in the Colab notebook but it only prints the result of the last statement in the cell.

UCLA Physics Department

17 of 27

Variables

  • Variables in code work like variables in algebra.
  • Variables can store a value(s) and these values may be updated.
  • Variables are case sensitive and cannot have spaces.
  • You can use the variables for other purposes later in the code.
  • Variables can also hold text (text is a “string” variable type)

UCLA Physics Department

18 of 27

Comments

  • Comments begin with # in Python.
  • 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 27

Numpy

  • Many functions in Python are stored in modules. We can import these modules and use these functions.
  • We will be using the numpy module for data analysis in this course.
  • For example, if we want to find the log of a number we use np.log()
  • A full list of numpy functions and how they are used can be found at the documentation page.

UCLA Physics Department

20 of 27

TA checkpoint 2 - Colab Notebook continued

Hit +Code to create a new cell.

  • In the same notebook where you printed your name, complete the following tasks.
    • Create a variable m = 5.
    • Create another variable v = 10.
    • Create another variable E = 0.5*m*v**2. (v**2 is v to the power 2)
    • Print E.
    • Write a comment about the steps you took to get the value of E.
    • Import the numpy module as np.
    • Print np.log(E)
  • Show your completed notebook to your TA.

20

21 of 27

Introduction to Arduino

UCLA Physics Department

22 of 27

Arduino Basics

  • A microcontroller that can interface between inputs and outputs
    • Example of input: set LED brightness to 200 from computer to Arduino
    • Output: turns on the LED to the specific brightness

UCLA Physics Department

23 of 27

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

24 of 27

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

24

25 of 27

Arduino Connection

UCLA Physics Department

26 of 27

Arduino Test

  • Once you have your Arduino, you should test it by connecting to your computer and following the instructions here.
  • You will need your Arduino for Week 2 onwards, so make sure you have it ASAP.

27 of 27

Beginner’s Programming Notebook (for practice only)

  • This introductory video (that you have seen on slide 15) how to perform basic calculations and use variables in Python.
  • This numpy video introduces you to the numpy module in Python that we will use extensively in the course.
  • Go over this Python notebook after completing all the lab material. Here you can practice what you have learned from the video lectures.
  • We strongly recommend students with very little programming background to practice typing code and running the cells to verify if things are working as expected.

27