In-class exercises for Fall 2024 #
1: Power an LED with “wall” power (Sep. 4) #
For a warm-up activity, let’s power an LED with “wall” power and control it with a push button.
Your goal is to create a circuit on your breadboard that lights up an LED with power from the wall, directed through your kit’s DC power supply. This circuit should have the following characteristics:
- It is implemented on a breadboard.
- It accepts power from a 2.1 x 5.5 mm plug from a 12 V wall adapter.
- It turns on an LED when a push button is pressed.
The components are:
- DC power supply
- One power (barrel) jack
- One LED
- One resistor (What size? See these notes.)
- One pushbutton
- Various wires and a breadboard
The image below is a circuit diagram representation of what you’re building.
Important note: the pins on the power jack are weird! More details below.
There’s a cryptic diagram in the datasheet for the PJ-102AH power jack, shown below.
One part is obvious: the pin in the middle is connected to pin 1.
But what is that little arrow/bump thing with pins 2 and 3?
That’s trying to tell you that when the jack is empty (i.e. no plug inserted), pins 2 and 3 are connected together. When you insert the plug, the barrel pushes on the bump at the end of the pin 2 contact, which bends it away from the pin 3 arrow, breaking the connection to pin 3. It’s so that you can detect whether there’s a plug inserted or not, which can be useful for battery-powered systems. You don’t need to implement that feature on your regulator board; you can just use pins 1 and 2.
In the cross-section view of a barrel plug inserted into the jack, you can see how the outer sheath of the plug makes contact with pin 2 and also how the plug breaks the continuity between pin 3 and pin 2.
Image credit: E. Schlaepfer and W. Oskay, 2023, Open Circuits, opencircuitsbook.com
For an extension to this exercise, re-arrange your circuit so you have one LED that stays on permanently and another that is powered by the button.
2. Build voltage dividers (Sep. 9) #
We’ve used resistors to limit current flow through an LED.
Let’s see how we can also use them to reduce a voltage so we can supply a specific lower voltage to a device. For example, we might be using a 12 V power supply but want to supply only 5 V to an Arduino or other microcontroller.
Read more here about using resistors as current limiters and voltage dividers.
For each of the set-ups below, provide 12 V as V_in, and predict the output voltage V_out.
After you’ve written down your predictions for V_out, build the circuits and use your multimeter to measure V_out. If your predictions were off, try to figure out why.
3: Build a voltage regulation circuit (Sep. 9 & 11) #
This is an introductory exercise designed to help you get familiar with breadboard prototyping while also building a basic circuit that will be useful for later exercises and projects. It forms the basis for Project #1, but you don’t need to think about that yet. If this is all new to you, look at the pages on breadboard prototyping and multimeters.
Your goal is to put 4 components and some wires on your breadboard to achieve this goal: 12 volts go in; 5 volts come out.
The 4 components are:
- One power jack
- One L7805CV voltage regulator
- Two capacitors
4: Matching schematics to breadboards (Sep. 16) #
Which of these breadboards has the same circuit as the one represented in the schematic?
5: Review of voltage and current (Sep. 16) #
For the circuit below, analyze the following first for when the switch is open, and then for when the switch is closed:
- Find sets of labeled points on the circuit where the current is the same.
- Identify any points on the circuit where no current is flowing.
- List the points in order from highest current flow to least current flow.
- Find sets of points where the voltage is the same.
- At what points should the voltage be near 0 V (e.g., ground)?
- Order the points from highest to lowest voltage.
6: Safety check (Sep. 16) #
If you feel a circuit component getting hot or starting to smoke, what should you do first? a. call over an LA b. take the component out of the breadboard c. unplug the power supply (e.g., barrel jack) from the breadboard d. use your multimeter to measure the voltage across the component
What is a possible result if your multimeter dial is turned to the “mA” symbol and you put your multimeter probes between a high and low voltage location on a circuit? a. you’ll get zero current reading on your multimeter and prevent your circuit from working b. you’ll let too much current go through your multimeter and blow its fuse c. you’ll hear a beeping sound warning you that you should use the “A” symbol and port
7. Control a motor with a BJT (Sep. 25) #
Making a motor spin is pretty simple: apply sufficient voltage across its leads. When we want to control things like motors (or LEDs, buzzers, etc.) electronically, instead of plugging them in to breadboards with our hands, we use transistors. The goal of this exercise is to use a bipolar junction transistor, the 2N3904, to turn on and off a motor.
You need 5 things in addition to the usual 12 V power supply and barrel jack:
- A 3.3 V voltage source, like the voltage regulation circuit you built previously
- A 2N3904 transistor (or really any transistor is fine, if you know the pinout)
- A DC gear motor
- An appropriately-sized current-limiting resistor for the BJT
The picture below shows the concept of what you’re building.
Extension: Add a potentiometer (a variable resistor) in series with the base and use it to vary the current flowing from base to emitter. Notice how this works as a crude motor speed controller, but also notice (carefully!) how the BJT heats up as more current is drawn through it.
8. Determine pin voltages and ideal resistor value for a BJT circuit (Sep. 25 & Sep. 30) #
- For the circuit shown in Exercise #7, fill in the table below to indicate the voltage at E, B, and C when the base resistor is connected to (a) 0 V and (b) 3.3 V.
- To run your kit’s gear motor, what is an ideal value for the resistor between 3.3 V and the base of the transistor?
V_E | V_B | V_C | |
---|---|---|---|
Input at O V | |||
Input at 3.3 V |
9. Control a motor with a MOSFET (Sep. 30) #
You’ve used a bipolar junction transistor to control a motor. Now try using the other main class of transistor: a metal oxide semiconductor field effect transistor, or MOSFET. The main difference between the two types of transistors is that BJTs are current-controlled, and MOSFETs are voltage-controlled.
You need 5 things:
- 3.3 V voltage source, which you can make using the 3.3V regulator in your kit
- 12 V voltage source
- An N-channel MOSFET
- A DC gear motor
- A pull-down resistor for the MOSFET
10. De-bugging challenges set 1 (Oct. 7) #
Images of circuits: https://tufts.box.com/s/8al3jwc3l81ewtb16ridfo2ewqbjtc3v
Your Circuit 1 responses - https://tufts.qualtrics.com/jfe/form/SV_bdOn4VWl8YvNoQm
Your Circuit 2 responses - https://tufts.qualtrics.com/jfe/form/SV_3WAXKdT2tT7u0rc
Your Circuit 3 responses- https://tufts.qualtrics.com/jfe/form/SV_3t9Kl4NmGOak5JY
Your Circuit 4 responses- https://tufts.qualtrics.com/jfe/form/SV_3sFQF0NE8VbxI58
11. KB2040 microcontroller challenges set 1 (Oct. 7 and labs) #
Learn to program your KB2040 board by working through the first 6 of these 12 challenges: http://andnowforelectronics.com/notes/kb2040-challenges/
You’ll want to consult the KB2040 programming resources.
For Kristen’s 1-page summary of the most important Circuit Python commands for your KB2040, click here.
12. Project 2 takeaways reflection (Oct. 9) #
(a) What are some problems you encountered along the way of working on your game for Project 2, and what did you learn from them? Let’s collectively document those here.
(b) Take a moment to fill out the self-assessment for Project 2, if you have not done so already.
13. Mechanically controlled two-way motor circuit (Oct. 9) #
With a partner or two, build a circuit that will allow you to use pushbutton switches to toggle a 12-V DC motor between the states of (a) OFF (b) ON counterclockwise, and (c) ON clockwise. That means your motor needs to be able to draw current in either direction (only one direction at a time). Your circuit should NOT use transistors. It should include (a) 12 V adapter and barrel jack, (b) DC gearmotor, (c) assorted wires, (d) as many pushbutton switches as you want.
As you work on this challenge, be careful not to create a situation where you are sending 12 V to ground only through wires. That’s a short circuit and will cause heat and smoke!
14. Recognizing common errors in KB2040 Challenge #6 (Oct. 16) #
Challenge #6 asks you to control a motor’s on/off state with a KB2040 output pin. Below are two approaches to setting up the circuitry. Each has problems that will prevent it from working correctly. What do you think those problems are?
What are the problems with approach #1?
What are the problems with approach #2?
15. H-bridge circuit analysis (Oct. 16) #
You’ve just wired up the top left corner of an H-bridge circuit using a P-channel MOSFET. You connect the other motor lead to ground so that you can test this corner before wiring up the transistors at any other corners. If this corner is wired correctly,
- What should happen to the motor when the MOSFET gate is connected:
- to 12 V?
- to ground?
- Sketch the current path for each of those gate states.
Now you add a BJT as a different method for switching on the P-channel MOSFET. Again, you want to test this corner before moving on to other sections of the H-bridge. If the BJT and P-channel MOSFET in this corner are wired correctly,
- What should happen to the motor when the BJT base is:
- at 3.3 V?
- at ground?
- Sketch the current path for each of those base states.
- What is the voltage at G for each of those base states?
16. H-bridge circuit analysis, part 2 (Oct. 21) #
This H-bridge circuit has four inputs, shown at locations 1, 2, 3, and 4. Each input turns a transistor on or off.
If all four switches were connected to 3.3 V (as shown below), what would happen and why?
17a. Measure a DC gearmotor (Power In) (Oct. 21) #
Here are a few basic measurements you can make to understand your DC gearmotor better. The overall goal is to determine the efficiency of the motor by comparing the electrical power that goes into it (“power in”) with the mechanical power it delivers (“power out”).
We’ll start with determing the minimum and maximum current that the motor draws.
- When your motor is stalled (like if you’re trying to lift something too heavy with it), it behaves like a resistor. This is when your motor draws maximum current, so checking the resistance is a good way to estimate the maximum current for whatever drive voltage you choose. Using the resistance-measuring setting of your multimeter (the omega symbol, Ω, represents ohms), measure the resistance of the coils of your motor.
- Make your DC gearmotor spin by connecting your motor directly to your 12 V DC power supply using alligator clips. Then, measure the current the motor draws with no load attached by putting your multimeter in series with the motor. You should switch your multimeter to the A setting (for Amps), and move the red lead to the port on the left side of the meter that is labeled “A”. The black lead stays in the black port labeled “COM”.
With measurement #1 above (the resistance of the coils), you can estimate the maximum current the motor will draw at any voltage. With measurement #2, you can find the minimum current at 12 V.
When you know the current and the voltage, you can multiply them to find the electrical power going into the motor.
If you can estimate how much power a task will require, you can start to figure out what voltage this motor would need to deliver that power (assuming perfect efficiency, for now). That’s the first step toward deciding whether this is the right motor for whatever you’re building.
17b. Measure a DC gearmotor (Power Out) (Oct. 23) #
The next step is to determine how much mechanical power the motor actually delivers (“power out”). One way to do this is to measure the time it takes to perform a certain amount of work (i.e., to add energy to a system).
- Use tape and string to hang a water bottle from your motor shaft.
- Measure the time it takes your motor to lift a known weight a specific distance (you could try something like 0.5 kg of water, lifted up 1 m). Compute the amount of work done in lifting the water. With these two values (work and time), you can find “power out.”
- Compare “power in” and “power out” to estimate the motor’s efficiency. You may want to measure “power in” at this motor operating point by measuring current while the water is being lifted.
18. Test the current limit of your P1 PCB (Oct. 23) #
The voltage regulators on your P1 PCB are specified (on their data sheets) to be able to handle current at the level of 1.5 A (the 5V regulator) and 0.8 A (the 3.3 V regulator). But the question is - once soldered into your P1 PCB, can they still perform up to their specified max current limit?
Test your P1 PCB by determining how much current your regulators can actually send out to loads at your output pins before going into thermal shutdown.
General procedure (details left to your group):
- Determine the resistance (it will be a low value!) you should attach to your 3.3 V and 5 V output so that something close to the specified max current is drawn (0.8 A or 1.5 A, respectively)
- Attach those low-ohm resistors to the output. These resistors stand in for a motor or other load to which you would want to supply 3.3 V or 5 V
- Monitor the voltage drop across that resistive load - that is, the voltage drop between the output of the regulator and ground
- Once that voltage drop begins to decrease lower than 3.3 V or 5 V, your regulator has gone into thermal shutdown (you may feel the regulators getting very hot before this happens)
Use this shared doc to post questions about the P1 PCB test and about P3 circuit board design.
19. Motor speed-torque curve (Oct. 28) #
Fill in values in the boxes to complete the motor speed-torque curve for the DC gearmotor in your ME 30 kit.
20. Motor selection cases (Oct. 28) #
For each of these potential motorized game designs, determine whether the mechanical power and torque needs are within the capabilities of the DC gearmotor in your ME 30 kit.
- Can your motor handle these designs with a direct-drive approach (i.e., no gear trains or pulleys)? Why or why not?
- If your motor can’t handle the design via direct-drive, can it do the job with a gear train or pulley transmission? Why or why not?
Work in a small group and post your answers to this google doc: https://docs.google.com/spreadsheets/d/18il0YS9v-0mt79J7DdHGnpuPw71HNZ7vh3b1nPuhzdM/edit?usp=sharing
21. Analog and digital sensors (Oct. 30) #
Circuits A and B show different ways to send a varying input to a KB2040 pin. Circuit A uses a photoresistor, a component whose resistance depends on the amount of light shining on it. Circuit B uses a simple pushbutton.
- Which of the set-ups below involves analog input to the microcontroller, and which involves digital input? Why? What will need to be different about your KB2040 code to measure voltage at A0 (in the photoresistor circuit) versus measuring voltage at D6 (in the push-button circuit)?
Build each of the circuits below and program your KB2040 to measure the input voltage from the point specified in the circuit. Answer the questions below.
- For Circuit A, vary the light level, and explore the range of voltages that your KB2040 pin receives (at the point specified in the schematic).
- How does the voltage at A0 relate to the light level (i.e., as the light level increases, does voltage at A0 increase or decrease)?
- How does the photoresistor’s resistance relate to the light level (i.e., as the light level increases, does its resistance increase or decrease)?
22. More practice with sensors and PWM (Nov. 4) #
Set up KB2040 challenge #10:
Use a resistor in series with your photoresistor to make a voltage that changes with light exposure. Use that voltage to control the speed of your gearmotor on pin D6.
Now try to add an LED whose brightness also varies with the light exposure to the photoresistor. Think carefully both about the code and the circuit components that you need to make this addition.
23. How to code your microcontroller to constantly check an input while also running actuators (Nov. 4) #
Suppose you want to check for the state of inputs while also running motors, lights, and other actuators.
In particular, try to do these three tasks at once:
- flash an LED (“led_1” in the code), 3 seconds on, 3 seconds off.
- constantly check for a button press that sets an input pin HIGH.
- flash a different LED (“led_2” in the code) when the button is pressed.
The naive approach #
Here’s a reasonable first attempt by a novice programmer to set up a microcontroller to check for input and flash a second LED when a button is pressed.
Why will this code probably not work very well to accomplish the goal stated above?
Try a state machine instead! #
Writing code for “state machines” is a better technique for this situation. At this link (and in the box below) is one way to set up state machines in CircuitPython.
State machine algorithms can be conceptually represented by circles and arrows. Each state is represented by one circle. Arrows indicate what happens immediately after the commands in one state have been completed.
In the diagram below, add labels to the arrows to represent the rules for moving between the “CHECK BUTTON” and “TOGGLE” states.
24. Using a state machine with a motor (Nov. 4) #
Imagine a Project 4 game with a 1-foot arm attached to the DC gearmotor shaft. Mounted at the end of the arm is a bowl. The motor switches directions every two seconds in order to move the bowl back and forth, switching directions every 2 seconds, as the player tries to toss a bean bag into the bowl. In the base of the bowl is a button whose purpose is to sense the presence of the bean bag. When the button gets pressed by the bean bag, the player wins! The motor stops moving and a celebratory green LED light blinks quickly 10 times.
Let’s set up a state machine for the code for this game.
Make a state machine diagram with circles and arrows to show:
- What are the two states needed in the main program?
- What are the rules for switching from one state to another, or for returning to the same state?
Draft the Circuit Python code to implement this state machine.
Sketch a circuit diagram that shows how you would wire up all the components.
25. Using a state machine with two inputs (Nov. 6) #
Set up KB2040 challenge #10 (where the speed of a gearmotor on pin D6 is controlled by the light exposure on a photoresistor).
Now, add a button that switches which direction the motor is going. How could a state machine help you implement this feature?
26. Stepper motor set-up (Nov. 6) #
With one or two partners, set up two working breadboard H-bridges. Choose one KB2040 and one stepper motor to use for this exercise. Hook up each coil of the stepper motor to one H-bridge. Connect one KB2040 digital output pin to each “diagonal” pair of H-bridge corners. You’ll use four KB2040 output pins in total. Control the motor using the code provided here: http://andnowforelectronics.com/notes/kb2040-programming/#stepper-motors
Note that you will need the adafruit_motor library. All CircuitPython libraries for the KB2040 can be downloaded here. Download the entire library bundle to your laptop, and then transfer ONLY the libraries you need to your KB2040.
27. More sensors and actuators (Nov. 6) #
-
Potentiometer for input; motor speed for output. Send the voltage output from a potentiometer (knob- or dial-like variable resistor) to an analog input pin on your KB2040. Use that varying voltage to vary the speed of a motor. Extension: Add a button that switches the direction of the motor (you’ll likely need state machine code for this.)
-
Ultrasonic distance sensor for input; motor direction for output. Send the output voltage from a distance sensor to an analog input pin on your KB2040. Use that distance signal to trigger whether your motor spins clockwise or counter clockwise. You get to decide on the distance threshold for switching directions.
-
Pushbutton for input; piezo buzzer for output. Use a pushbutton to send either 3.3 V or 0 V to a digital input pin on your KB2040. Use that button signal to trigger the sound of a buzzer. Power the buzzer with PWM. Explore different frequency and duty cycle values, and see how that changes the sound of the button.