1 of 62

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

UCLA Physics Department

1

University of California, Los Angeles

Department of Physics and Astronomy

​

Physics 4AL

​

2 of 62

Course Objectives

By the end of the quarter, you will be able to:

  • Work in a group to conduct your own scientific research project
  • Use an ESP32 (hardware) to collect and wirelessly transmit experimental data to your computer using:
    • Ultrasonic sensor and accelerometer
    • Bluetooth module
  • Use Python to perform data analysis, including:
    • Error analysis and propagation
    • Curve-fitting data to user-defined functions

2

3 of 62

Course Objectives

To conduct your own research project, you will learn how to:

  • Collaboratively design a new research project
    • Work in a group to design a good scientific hypothesis
    • Design the experimental procedure to test your hypothesis
  • Conduct a research project and report on your results
    • Collect experimental data with the ESP32
    • Analyze the experimental data using Python
    • Evaluate your hypothesis by quantitative analysis
    • Write a scientific report to communicate your results

3

4 of 62

Outline of Lab 1A

  • 1A In-Lab
    • Logistics.
    • Intro to Python.
    • Intro to Multimeter and Circuit Elements.
    • Intro to ESP32

​

​

​

UCLA Physics Department

4

5 of 62

Logistics

UCLA Physics Department

5

6 of 62

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.
  • The lab sections & office hours: https://www.uclaphysics4labs.org/4al-lab-sections-office-hours.html
  • Virtual office hours (when available) - help with coding, data analysis, and report writing.
  • Encouraged to go to any office hours you can make it to
  • Main point of contact: Your section TA
    • Notifying Absence
    • Questions about assignments, etc.

​

​

UCLA Physics Department

7 of 62

Class Schedule

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

​

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

​

​

​

UCLA Physics Department

7

8 of 62

Assignments

  • There are two kinds of assignments (15% + 15%) if submission is required.
    • Pre-labs that are due 2 hours before the first lab session weekly.
    • Post-labs that are due on Sundays after the second lab weekly at 11:59 pm.
  • A team report is due at the end Unit 2 (10%).
  • A team report is due at the end Unit 3 (15%).
  • The Final Project includes a Presentation (15%) and Written Report (15%).
  • For group assignments all group members must split work evenly
  • All Assignment submissions: PDF to Gradescope.
  • Attendance is worth 12%.
  • Attendance: Max 4 missed unexcused absence before dropped.
  • Cleaning up lab/table is worth 3%.
  • All of the above sum to 100%.

Grading info: https://www.uclaphysics4labs.org/4al-grading-policy.html

​

​

UCLA Physics Department

9 of 62

Online Lab Resources and Format

​

​

  • Gradescope
    • Assignments: PDF submission for grading
    • Join gradescope through the bruinlearn coursepage
    • 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

10 of 62

Miscellaneous Administrative Information

Section Switching

  • Often students ask about switching sections with another student, this is not a supported feature of myucla.
  • This means we CANNOT switch students between sections
  • For students who want to switch sections, if there is availability on myucla, they are welcome to make that switch themselves. This should be communicated to both section TAs if done.

Course Questions

  • For questions about course material, assignment due dates, absences, etc you should first reach out to your section TA
  • If you do not hear from them within a reasonable timeframe, you should forward said email then to the lead TA, and then to Professor Arisaka
  • You can find your TA’s email address here
  • For more serious matters it is recommended to directly reach out to Professor Arisaka

10

11 of 62

Introduction to Python

UCLA Physics Department

11

12 of 62

Python

  • Analysis on experimental data will be done via Python.
  • Python:
    • an open source programing language
    • designed to be simple to write and understand

​

12

13 of 62

Using Juypter

Log in link:

https://lab.onescienceplace.org/

​

Please note that material pulled from this link will be periodically updated as the course is changing throughout the quarter. Your TA will tell you when and if changes were made so that you can update you repos.

Sign in with your ucla gmail account. If you do not know the ucla gmail address which ends with a @g.ucla.edu, please check at https://g.ucla.edu/

​

​

​

​

​

14 of 62

click

15 of 62

click

16 of 62

click

has to be Google

17 of 62

Needs to end with g.ucla.edu

18 of 62

19 of 62

“Open app session” button might take 1~2 minutes to appear

20 of 62

Can access class material inside Physics_4AL/

21 of 62

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.

​

​

21

22 of 62

Python Notebooks

22

Google Collaboratory Notebook

Input Code

Output

23 of 62

TA checkpoint 1 - Python 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.
  • This should be done in “Post-Lab-Week-1.ipynb”, located in the Assignments/Post-Labs/ Subfolder (see right image above)

​

​

​

23

24 of 62

Numbers

  • Basic calculations like addition, multiplication etc.
  • Print numerical values:
    • do not use single or double quotes.
  • We can print numbers without using the print() in the notebook:
    • Only prints the result of the last statement in the cell.

UCLA Physics Department

25 of 62

Variables

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

UCLA Physics Department

26 of 62

Comments

  • Begin with # in Python.
  • For the human reader to understand what the code does.
  • When the code is compiled, these comments are not read by the computer (Nothing in comment is done).

​

​

​

UCLA Physics Department

27 of 62

Numpy

  • Modules:
    • Many functions in Python are stored in modules. Import these modules to use these functions.
  • Numpy module:
    • Use it 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

28 of 62

TA checkpoint 2 - Using Variables and Numpy

  • In the same notebook (“Post-Lab-Week-1.ipynb”) 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.

​

​

28

29 of 62

Introduction to circuit components

UCLA Physics Department

30 of 62

Electrical Components

  • Resistor

​

  • Capacitor

​

  • Inductor

​

  • Diode (LED)

Name of Component Electrical Diagram Actual Component Arduino Schematics

31 of 62

Component Polarity

Polarized

Non-Polarized

  • Polarity: Positive and negative pins to be mounted in the right orientation
  • When not connected right, can cause component failures, can disrupt the functionality, circuit can become non-functional

32 of 62

Breadboard

  • Divided down the middle.
  • Power rails on both sides (shown by blue and red columns).
  • Rows are connected (shown by black lines).
  • Columns A-E, F-J 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.

33 of 62

Breadboard

​

34 of 62

Introduction to ESP 32

UCLA Physics Department

34

35 of 62

ESP32

  1. Digital & Analog (GPIO) pins - inputs and outputs

(specify by pinMode in code)

  1. GND and 5V pins
  2. USB port
  3. Reset/Boot button

35

URW 5/21/24

24 Summer Training

New Setup !!!

36 of 62

ESP32 Input/Output

36

37 of 62

ESP32

UCLA Physics Departmentc

37

38 of 62

ESP32 Connection (Windows)

UCLA Physics Department

38

Check if the computer can find the device. It should download CH340 automatically once connected. If not, follow further instructions.

You can also go to the start menu and type “Device Manager”

39 of 62

ESP32 Download

​

UCLA Physics Department

39

40 of 62

ESP32 Connection (Windows)

UCLA Physics Department

40

41 of 62

ESP32 Connection (Mac)

UCLA Physics Department

41

41

Download the CH34x Mac Driver

Open CH341SER_MAC.ZIP to extract the driver folder.

Open CH34xVCPDriver.pkg to start the macOS installer.

The folder also includes a PDF with installation instructions.

42 of 62

ESP32 Connection (Mac)

UCLA Physics Department

42

42

Complete the Driver Installation

1

Choose Continue

The installer identifies the CH34x VCP driver and shows version V2.1.

2

Proceed through the installer

Continue through Destination Select, Installation Type, and Installation.

3

Complete Installation

Follow the macOS prompts until the installer reaches the Summary screen.

43 of 62

43

10

4

Confirm Successful Installation

When this confirmation appears, choose Close. The CH34x driver is installed.

44 of 62

Troubleshooting for Mac

  • If you aren’t able to upload sketches, after following tutorial directions to download the drivers (CH34x), go to System Settings > Privacy and Security > Allow, then reset computer and Arduino IDE for the drivers to work

45 of 62

Arduino

UCLA Physics Department

45

46 of 62

13

Downloads and references

  • Arduino IDE 2.3.10

Use the current Arduino IDE 2 release. The old 1.8.x Legacy IDE is no longer required for this course.

  • References

4AL/4BL software versions

Install these through Boards Manager or Library Manager.

ESP32 by Espressif Systems

3.3.12

Adafruit MPU6050

2.2.9

Adafruit BusIO

1.17.4

Adafruit Unified Sensor

1.1.15

Library Manager can install required dependencies automatically when prompted.

UCLA Physics Department

47 of 62

15

IDE Environment Configuration

1 Add the ESP32 source

Open Arduino IDE Settings.

​

Find Additional Boards Manager URLs.

​

Add the stable Espressif package URL.

2 Install the ESP32 platform

Open Tools, Board, Boards Manager.

​

Search for esp32.

​

Choose esp32 by Espressif Systems and install it.

3 Confirm the version

For this course, use the stable ESP32 Arduino Core version 3.3.12.

​

Restart the Arduino IDE after installation.

Stable package URL

The stable index used by the IDE is:

https://espressif.github.io/arduino-esp32/package_esp32_index.json

Boards Manager

esp32

esp32 by Espressif Systems 3.3.12

Version check

Boards Manager should show 3.3.12 as installed.

✓

UCLA Physics Department

48 of 62

16

IDE Environment Configuration

Final setup for the ESP32 WROVER boards used in 4AL

1

Connect the ESP32 to the computer with a USB data cable.

2

Open Tools, Board, esp32, then select ESP32 Wrover Module.

3

Open Tools, Port and select the serial port associated with the ESP32.

4

Upload a simple test sketch and open the Serial Monitor to confirm communication.

Board

ESP32 Wrover Module

✓ Ready

Mac note: if no serial port appears, install the current WCH CH34x macOS driver, then reconnect the board.

UCLA Physics Department

49 of 62

Downloads and references

  • Arduino IDE download (Scroll down to Windows or Mac options)
  • Arduino Reference
    • https://www.arduino.cc/reference/en/
    • Contains all the references for functions and variables

49

50 of 62

Arduino IDE

50

51 of 62

IDE Environment Configuration

51

https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json

​

Mac: Arduino -> Settings

52 of 62

IDE Environment Configuration

52

If everything’s right !

​

Make sure to select this board for the rest of this course!

​

53 of 62

Introduction to Digital Multimeter

UCLA Physics Department

54 of 62

Digital Multimeter

55 of 62

Potential measurements with Digital Multimeter

DC Voltage

AC Voltage

Resistance

Continuity

Capacitance

Frequency

Current (smallest)

Current (medium)

Current (largest)

Use left input

56 of 62

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.

​

57 of 62

TA Checkpoint 3 - Resistance measurements

57

  • 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 (Ω)

​

​

​

​

​

​

58 of 62

Testing ESP32 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.

58

59 of 62

Testing ESP32 with multimeter

​

  • Measure the DC voltage on the ESP32 pins:
    • Put the red lead to the location you want to measure like the 5V pin on ESP32 (+ 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.

59

60 of 62

TA Checkpoint 4 - Voltage Measurements

Measurement

Voltage (V)

5 V Pin (instructions in slide 36)

​

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

​

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

61 of 62

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.
  • Very little programming background? Practice by…
    • typing code
    • running the cells
    • verify if things are working as expected

​

​

61

62 of 62

Post-Lab Requirements for lab 1A

  • Your post lab for Lab 1A will include exercises from Checkpoints 1-4. You can transfer work down from other notebooks and paste them into your Post-Lab notebook.
  • Please complete the post lab assignment found in your JupyterHub Repo.
    • Physics_4AL_2023-24/Assignments/Post-Labs/Post-Lab-Week-1.ipynb

62