# Lab 1: Introduction

The questions below are due on Sunday July 15, 2018; 11:59:00 PM.

You are not logged in.

Intro to Lab

## Goals:

Goals: In this lab we're going to build some simple circuits using LEDs, resistors, and switches. We'll then build an audio amplifier that we can use to play music from our our phones (if you have a 3.5 mm plug) and/or use the Raspberry Pi as an alternative. In Lab 02, we'll use Python to build a simple music player with some controls, merging hardware and software.

## 1) Hardware

We'll be using the Raspberry Pi in this class. This is a basic standalone computer, except it has a number of external interfaces that we can use to generate electrical inputs and outputs as needed. Inputs can be things like keyboards, but they can also be more flexible, and these flexible inputs and outputs are what we'll be spending a lot time working with. By using electrical components, we can then interface with the outside world and pretty much do anything we want.

Today we won't focus too much on interfacing our Pi with electrical components, but instead we'll get some circuit experience. After that (and pretty much every lab for the future), we'll be working with both hardware and software and how they merge together to create complex systems.

Most of our circuit building will take place on what we call a "breadboard. The breadboard is a convenient prototyping device which allows us to quickly build up circuits rather than have to rely on twisting component leads together.

The breadboard can be a little hard to understand at first, but it really doesn't need to be. We'll be using the breadboard a lot, so it is good to learn how it operates. Each hole in the breadboard goes down into a metal grabber thing. The grabber part allows electrical parts to be inserted and held in place. Underneath are small strips of metal which generlaly have several grabbers associated with them so two or more electrical parts can be held onto the same conductor, and therefore be electrically connected. Often regions of continuous conductor are called a node. The way the metal is wired up in the breadboard is shown in the Figure below. The green boxes show example sets of holes that are connected together.

Answer the questions about a few breadboard connections below. Make sure to review what the word node means.

Enter your answers as capital letters separated by commas only (if the correct answer is B then enter 'B' below.. If there are more than one correct node enter all...so that if the correct answer is B and L then enter: 'B,L'. Enter None if there are no other connected nodes.

What other nodes are connected to Node A?

What other nodes are connected to Node D?

What other nodes are connected to Node H?

What other nodes are connected to Node K?

What other nodes are connected to Node F?

Sometimes we'll call the really long nodes "rails" or "busses" depending on the situation. It doesn't change what they are!! They are still nodes
.

Some of you may have slightly different numbers of nodes in your breadboard so the answer below only needs to be approximate

How many total electrical nodes are located on your breadboard (counting the long ones on both sides?)

### 1.2) Breakout Board

The Pi is connected to the outside world through a ribbon cable that then connects to a board known as a "Breakout". The Breakout, shown in the image below, gives us nice, clean, readable access to all the Pi's pins. These pins are located in the breadboard, which means we can connect other parts and/or wires to those pins as needed!

In the photo above, two wires are used to connect the 3V3 pin and the GND pin of the Raspberry Pi to the + left rail and the - left rail (long nodes) of the breadboards. The 3V3 pin is positive voltage (of 3.3V). The GND is. Very often it is a good idea to use the really long nodes on both sides of the breadboard as a way to distribute power and ground across the breadboard so you don't need to use really long wires all the time.

When we draw out the breakoutboard in this lab and later labs we'll often just represent it as a grey box.

## 2) Components

We need a way to effectively convey things to build, and pictures generally aren't good at this. Instead we'll use schematics where we clearly show how components and specific nodes are connected. Each important part is represented by a schematic symbol and they are linked using wires (single straight lines). There are five circuit elements that we'll work with now (and more later). Each schematic symbol and what they represent is discussed briefly below.

#### 2.0.1) Voltage

This symbol represents a connection to the positive voltage of 3.3V. It is accessible via the 3V3 pin on your breakout board. In conjunction with the GND shown next, provide the power for most of our work and experiments. Since we'll often need to make many connections to 3.3V it is often a good idea to distribute this voltage out using the "+" busses on both sides of the breadboard.

#### 2.0.2) Ground

This symbol represents a connection to the ground voltage of 0V. It is accessible vias the GND pin on your breakout board. In conjunction with the 3V3 pin (which provides +3.3V), these pins provide most of the power for our system and experiments (providing a difference in electrical potential. Since we'll often be making many connections to ground it is often a good idea to distribute this voltage across the breadboard by using the "+" and "-" busses on both sides of the board. You can see us doing that in Figure 5 above.

#### 2.0.3) Resistor

This is the symbol for an electrical resistor, which is a device that resists the flow of electricity. In real life, resistors are the small tan cylinders with different colors on them. The colors tell us what value they are (in Ohms). Don't worry about memorizing that color code. We'll write python code to figure it out for us eventually. It doesn't matter what direction you plug a resistor in! For Lab 01, you'll want the following:

• 300\Omega resistors: Red-Black-Brown-Gold
• 5.1K\Omega resistors: Green-Brown-Orange-Gold

#### 2.0.4) LED

Light-Emitting Diodes (LEDs) are small do devices that do basically what their name implies: emit light. LEDs can be many different colors as well as emit radiation in the ultraviolet and infrared spectrum. The schematic symbol for an LED is a triangle with a line next to it. The "D" part of LED stands for "diode" (it is a light-emitting diode), and diodes have to be hooked up in a certain direction like shown in the figure below:

Additionally, LEDs generally need to be hooked up in series with a resistor when being connected to a voltage source like shown below. Otherwise the LEDs may blow up.

#### 2.0.5) Switch

The final electrical component we'll be talking about for now is the switch. Its schematic symbol is the following:

There are many, many types of switches, and you'll gain some experience with several of them. What they all do, however is selectively control electricity based on mechanical force (by making two pieces of metal either touch or not touch). For today's lab we'll be using what are called "Normally Open Momentary" switches. This means that when left untouched, the two terminals of the switch are electrically disconnected, but when you press on the button, the two terminals electrically connect. In real life our switches look like what is shown in the figure below, where we've also provided an image of the switch in the context of the breadboard.

Pay attention to the the direction of the switch in the images below!!! Having the switch rotated by 90 degrees from the way it is in the image will result in a non-working switch. It also won't really fit when rotated sideways so use that as a guide.

These types of switches have only two electrical nodes inside them, but four electrical contacts and legs. This means there is some redundancy in them. Pay attention/use that as you wire up circuits below.

## 3) Let's Build

OK let's build some stuff. First off, let's create a simple LED circuit with a resistor. Pick a Red LED from up front and then pick a 300 \Omega (ask for help if you can't find label) resistor. Use these parts and some wires to build the following:

When completed the circuit should glow. When it is glowing and you're happy with the state of things, replace the 300 Ohm resistor with a 5.1Kilo-Ohm (5.1K) resistor...(ask for help if you can't find it). What do you observe? Remember this for when you discuss with staff.

Now replace your 5.1K resistor with the original 300 Ohm one so it is back to how it was. Add a second white LED D2 in paralallel to the your first LED. What do you observe?

Remove the white LED and replace it with a second red LED. What do you observe?

It turns out different color LEDs require different voltages across them to operate, and it is often very difficult (or impossible) to run two LEDs of different types when in parallel. A more correct way to do it is to add a second isolated path with its own resistor. Build the circuit below where D1 is a red LED and D2 is a white LED.

Flag a staff member and discuss what you observed in the experiments above for a minute or two.

Mkay, let's add in some switches. Build the circuit below (with whatever color LED you want). What should this circuit do? What does this circuit do after you build it?

All right. With two switches, build a circuit that glows the LED only when BOTH switches are turned on (pushed). We call this an AND circuit (because it only produces a positive output when input 1 AND input 2 are pushed.) When it is working, save this circuit for your final video (see at the bottom of the page).

Build the next part on a separate part of your board:

Good. Now you've got some circuit prototyping experience.

## 4) Audio Amplifier

OK now that we've got some basic circuits out of the way, we're going to build an audio amplifier. Shown below is the schematic of the amplifier we're constructing. Yes it might look nasty, but don't worry...it is the same as above, just with more stuff and more components. We don't expect you to know how to build this off the bat, so first we're going to discuss each of the new parts that are in it.

#### 4.0.6) TRS Connector

First up on the left side of the schematic above is a symbol that should look familiar to you millenials. It is a Tip Ring Sleeve connector, which is usually abbreviated as "TRS" connector. This is the same piece of equipment which you usually use to get music from your phone into your headphones1 It is comprised of three conductors separated by two plastic insulators. The conductor regions make pressure connections inside the phone with tiny pins that provide the left audio voltage (for your left speaker/earbud), the right audio voltage (for your right speaker/earbud), and a ground connection. The TRS connector fits into a TRS socket very similar to a key in a lock. We're going to merge the left and right wires (orange and red) wires together here. The result is that our amplifier will be mono rather than stereo. If you want to incoporate a stereo amp into your final project later on that's definitely a possibility.

#### 4.0.7) Potentiometer

The next part of the schematic is this device, which looks like a resistor in a gray box with an arrow pointing to it. This similarity is well-justified.

This part is known as a potentiometer (or "pot"). It is made up of two different resistors which when combined form a mini-circuit that can divide voltage (you'll start doing the math for voltage dividers in exercise 03!). The division amount is based on the turn-angle of the potentiometer. In this circuit the voltage we're dividing is an audio signal, so by turning the potentiometer we're changing the amplitude of the audio signal! This means we're changing the volume! In real life for this lab the potentiometer looks like this:

And that links to what is drawn in the schematic with the following:

The legs on the potentiometer are not randomly assigned! (The middle arrow leg in the schematic coresponds to the middle leg in real life, while the two side legs can be flip-flopped as needed). So as you build you circuit please pay attention to which one you hooking up to what part. This is really important!

#### 4.0.8) Amplifier Chip

The next device you see in the schematic (going left to right) is the triangle-shaped thing which is an audio amplifier. The audio amp that we'll be using looks like the following in real life:

The audio amplifier we're using is known as an LM386-N1 and it exists in an 8-Pin DIP. The "8-Pin" part comes from the fact that it has 8 legs. The DIP part comes from the term Dual Inline Package. The pins on a DIP type device start in the lower left and go counter-clockwise around:

It is useful to be able to read the text on the chip since the LM386 is only one of hundreds of thousands of types of DIPS that all look nearly identical except for number of pins and text on them. The diagram below explains a bit more about how to read DIP chips:

To use the LM386-N1 (or any DIP you'll want to place it across midline gap so that each of the package's eight legs have their own individual node on the breadboard. On the schematic for the audio amplifier the pin numbers are labeled. So for, example, Pin 6 goes to 3.3V.

#### 4.0.9) Capacitor

The capacitors we use in this class are non-polar capacitors, which means you can hook them up in either direction:

We have three capacitors in our schematic, labeled C1, C2, and C3. We'll be using one capacitor (C3) as a bypass capacitor, the purpose of which is to minimize power supply noise. It sort of acts like a power supply water tower, absorbing spikes in power and providing sufficient voltage when there are power dips (which happen all the time). The result is a constant "water pressure" of voltage for the circuit so there aren't any noise problems. You'll need to place it close to your LM386-N1 chip like shown below:

Capacitor C1 and C2 are used to allow only the audio signal through.

#### 4.0.10) Speaker

The speaker is where noise comes out. It is an eletro-audio transducer, converting a varying electrical field (linked to the varying voltage) to a varying magnetic field which then causes movement of a mechanical diaphram which pushes and pulls air back and forth causing sound waves to form. Our speaker looks like the following in the schematic and in real life, respectively:

## 5) System Overview

So again, now that we've reviewed each schematic component, let's take a look at the overall schematic again:

You should be able to see a rought left-to-right flow of signal. On the left side, audio from some source such as your phone or the Raspberry Pi comes in through the TRS connector, before being adjusted using the potentiometer. This adjusted signal is then fed into the audio amplifier chip (LM386-N1), and then being used to drive a speaker that provides sound. This step-by-step discussion of the amplifier emphasizes the "stages" of the device. The high level stages of the whole amplifier are shown below:

## 6) Gain

When you build this circuit you'll be able to produce music at varying volume by turning the potentiometer. When you vary the volume of this system you are varying the Gain of the amplifer. Gain of an amplifier is usually described by the variable A. The total gain of an amplifier is A_{tot}. If our input music signal is a voltage V_{in} and our amplified output signal is V{out}, we can relate the two of them by using the gain such that:

V_{out} = A_vV_{in}

Gains can often be sketched out as being represented by triangles (one triangle for each stage). The total gain of our amplifier comes from the series accumulation of several gain stages, in particular the gain of the potentiometer stage (which varies from 0 to 1 based on the angle that you've turned it to), and the gain of the LM386-N1 chip which is ideally fixed at 20. When gains are in series with one another their total gain is their product as shown below. For example if you had three gain stages in series, A_1, A_2, and A_3, the total gain of the system would be their product A_1A_2A_3:

Using what we just learned and the system diagram above, what is the lower bound of gain in our overall amplifier circuit?

Using what we just learned and the sytsem diagram above, what is the upper bound of gain in our overall amplifier circuit?

If you hook a capacitor from pin 1 to 8 on the LM386 you can boost the gain of the chip to 200. If we do that and if we set the potentiometer to be at 25% of maximum, what is the gain of our overall system?

## 7) Build It!

OK time to build your amplifier circuit. Follow the schematic as best you can!

Keep your wires short. Doing this will minimze noise! (you may not believe me but I swear to god it will)

## 8) Audio Playing

Now that your amplifier is working, feel free to plug in your phone and play some jams. No more than three F-words per song, please. Thanks.

If you're too cool for school and have a modern phone sans-TRS connector, you can instead play some music off of your Raspberry Pi! Go to the Raspberry symbol> Programming > Python3 (IDLE)

Then go to File>New. Paste in the code below, save it as something other than asdf.py and go to Run > Run Module.

Music should start playing (a song by Tyler the Creator from his very good album Flower Boy). It will keep playing until you press Control-C (Control-C is what's called a "Keyboard Interrupt").

import pygame

try:
pygame.mixer.init()
pygame.mixer.music.play()
print('Playing')
except KeyboardInterrupt:
pygame.mixer.music.stop()
print('Stopped!')


The audio amplifier takes in music, or more generally sound, encoded in a varying voltage over time. The speaker is what converts that varying voltage to varying mechanical positions, which in turn get transferred into vibrations in the air (aka sound). We've earlier seen that LED's are components that glow based on the electricity applied to them, so we can actually use them to sort of visualize the sound we're producing. Modify your circuit like shown below and observe what happens.

Do not take apart your circuit! We'll be using it in most future labs!

## 10) Final Deliverable (Lab 1)

When you're all done, create a short video and upload to Youtube/Google Drive, showing the following behaviors:

• Your functioning AND Switch Circuit
• Your audio amplifier playing music!
• Your potentiometer changing the volume
• The LED flashing as the volume gets cranked.
• Anything else you want to show.
Problem Status:
A Python Error Occurred:

Error on line 2 of Python tag (line 508 of source):



Enter the url for the video

Place all of yoru and server code as a zip file here:

No file selected

Enter any comments you may want me to know about. For example, you can list non-obvious things you got working, things you're proud of, outstanding issues, etc.. Please hit submit on this question even if you don't have any comments.

Footnotes

1though this is less and less the case after Apple got rid of it in their latest phones. (click to return to text)