Day 8: (Mon Sep 26, Week 5) Child as Scientist; Motor Tests

Notes for 2022-09-26.

Notes from Day 7

I never did send out a reading assignment as mentioned.

New Assignments

  1. New assignment, due in a week: Exercise: Motor Marble Run Tile

  2. Preview of observation worksheet for Wednesday: Children’s School Classroom Observation Session.

Administrative

Please note: on Wednesday we will meet directly at the Children’s School, MMC-17. Please meet at the south lobby, one floor down from the rotunda entrance. Please be prompt, we will enter the secure school area precisely at 12:20PM.

Agenda

  1. Demo the 3D-printed fidget toy results.

  2. Introduce the new assignment.

  3. Discuss the Children’s School Classroom Observation Session on Wednesday.

  4. In-class experimentation and testing.

    • set up photoreflector circuit (directly on breadboard to start, later with soldered wires)

    • set up DRV8833 driver circuit (possibly soldering on header pins)

    • set up power adapter and barrel adapter

    • try driving a gearmotor from CircuitPython

Reference Materials

Lecture Demo Code

 1# photomotor.py
 2#
 3# Raspberry Pi Pico - photointerrupter and DC motordemo
 4#
 5# Demonstrates operating a DC motors driven by a DRV8833 based
 6# on a photointerrupter signal.
 7#
 8# This assumes a photointerrupter circuit is wired to analog input 0.
 9#
10# This assumes a Pololu DRV8833 dual motor driver has been wired up to the Pico as follows:
11#   Pico pin 24, GPIO18   -> AIN1
12#   Pico pin 25, GPIO19   -> AIN2
13#   Pico pin 26, GPIO20   -> BIN2
14#   Pico pin 27, GPIO21   -> BIN1
15#   any Pico GND          -> GND
16# DRV8833 carrier board: https://www.pololu.com/product/2130
17
18################################################################
19# CircuitPython module documentation:
20# time    https://circuitpython.readthedocs.io/en/latest/shared-bindings/time/index.html
21# math    https://circuitpython.readthedocs.io/en/latest/shared-bindings/math/index.html
22# board   https://circuitpython.readthedocs.io/en/latest/shared-bindings/board/index.html
23# pwmio   https://circuitpython.readthedocs.io/en/latest/shared-bindings/pwmio/index.html
24
25################################################################################
26# print a banner as reminder of what code is loaded
27print("Starting photomotor script.")
28
29# load standard Python modules
30import math, time
31
32# load the CircuitPython hardware definition module for pin definitions
33import board
34
35# load the CircuitPython interface to the analog-to-digital converter
36import analogio
37
38# load the CircuitPython pulse-width-modulation module for driving hardware
39import pwmio
40
41# load the drv8833.py module, which should be copied to the top-level of CIRCUITPY
42from drv8833 import DRV8833
43
44#--------------------------------------------------------------------------------
45# Create an object to represent a dual motor driver.
46print("Creating driver object.")
47driver = DRV8833()
48
49# Create an object to represent the ADC0 input, which is physically pin 31.
50# E.g., this may be attached to photocell or photointerrupter with associated pullup resistor.
51sensor = analogio.AnalogIn(board.A0)
52
53#--------------------------------------------------------------------------------
54# Begin the main processing loop.  This is structured as a looping script, since
55# each movement primitive 'blocks', i.e. doesn't return until the action is
56# finished.
57
58print("Starting main script.")
59while True:
60
61    # Read the sensor once per cycle.
62    sensor_level = sensor.value
63
64    # Map to a motor activation level between -1.0 and 1.0.  The Pico has 12-bit
65    # analog-to-digital conversion so the actual conversion has 4096 possible
66    # values, but the results are scaled to a 16-bit unsigned integer with range
67    # from 0 to 65535.
68    pwm = 2 * (2**16 - sensor_level) * (1.0 / 2**16)
69
70    print("PWM:", pwm)
71
72    # Set the motor speed.
73    driver.write(0, pwm)
74
75    # Slow the printing rate.
76    time.sleep(0.1)