COMPUTER TECH with Mr. Mills
Explanation
A pushbutton acts as a bridge in a circuit that can be opened and closed. When a button is pressed, it completes the circuit between the left half of the button and the right half, allowing electricity to flow from the voltage source (red wire) to a path to ground (black and green wires). To ensure we are not causing a short circuit, we use a attach a resistor to the ground end of the circuit, so that electricity can safely pass through. The green wire, in a sense, intercepts the signal and can be detected with a digitalRead() on the pin of your choice.
1 int buttonVal = 0;
2 const int buttonPin = 2;
3 void setup(){
4 pinMode(buttonPin, INPUT);
5 }
6 void loop(){
7 buttonVal = digitalRead(buttonPin);
8 if (buttonVal == HIGH){
9 //do something
10 } else {
11 //do something different
12 }
13 }
Explanation
The first variable declared on line 1 is saved as an "empty" placeholder that we "fill" with a zero. We call it "buttonVal" in this example, but it could be anything. The constant "buttonPin" declared on line 2 is the pin we will wire our button up to. This could be any digital pin on the arduino, but we've chosen 2 here as an example. We use a constant so that we can reference it easy later on.
SETUP
On line 4, we initialize pin 2 on the Arduino as an input, calling the constant we just declared.
LOOP
On line 7, in the loop function, we change the value of the variable "buttonVal" to whatever we are reading on pin 2, which could be LOW (0) if the button is not being pressed, or HIGH (1) if it is being pressed. On line 8, we use a conditional statement (IF) to check to see if the button is being pressed by comparing buttonVal to HIGH. If this variable is HIGH, the statement returns TRUE and the Arduino executes line 9 of the code, which we've left blank here. If the statement on line 8 returns FALSE (i.e. the button is not being pressed) then we skip line 9 and execute line 11 instead, which has also been left blank in this example.
Explanation
A slideswitch is very similar to a pushbutton, acting as a bridge in a circuit that can be opened and closed. A slideswitch has two settings, left and right. The left side of the switch shown in the example does not complete any circuit, so no electricity flows. The right side of the switch forms a completed circuit, flowing the ground through the green wire and the resistor. The left side of the switch can also be wired up to another pin if desired.
1 int switchVal = 0;
2 const int switchPin = 2;
3 void setup(){
4 pinMode(switchPin, INPUT);
5 }
6 void loop(){
7 switchVal = digitalRead(switchPin);
8 if (switchVal == HIGH){
9 //do something
10 } else {
11 //do something different
12 }
13 }
Explanation
The first variable declared on line 1 is saved as an "empty" placeholder that we "fill" with a zero. We call it "switchVal" in this example, but it could be anything. The constant "switchPin" declared on line 2 is the pin we will wire our switch up to. This could be any digital pin on the arduino, but we've chosen 2 here as an example. We use a constant so that we can reference it easy later on.
SETUP
On line 4, we initialize pin 2 on the Arduino as an input, calling the constant we just declared.
LOOP
On line 7, in the loop function, we change the value of the variable "switchVal" to whatever we are reading on pin 2, which could be LOW (0) if the slide switch is in the left position, or HIGH (1) if it is in the right position. On line 8, we use a conditional statement (IF) to check to see which side the switch is by comparing buttonVal to HIGH. If this variable is HIGH, the statement returns TRUE and the Arduino executes line 9 of the code, which we've left blank here. If the statement on line 8 returns FALSE (i.e. the switch is on the left) then we skip line 9 and execute line 11 instead, which has also been left blank in this example.
Explanation
A potentiometer is a dial that increases resistance in a circuit the more it is turned, and the resulting current can be measured using a "wiper" plugged into any analog pin (shown in green here). If the dial is turned all the way to the left, almost no current flows through the wiper (green wire), instead flowing straight to ground (black wire). As the dial turns clockwise, less resistance is applied and more current flows through the wiper (green wire) and less flows through to ground (black wire). The result is a range of values from 0V to 5V that can be detected by the Arduino using an analogRead(). The arduino reports this value as a 10-bit integer from 0 to 1023, 0 meaning 0V and 1023 meaning 5V.
1 int potVal = 0;
2 const int potPin = A0;
3 void setup(){
4 pinMode(potPin, INPUT);
5 }
6 void loop(){
7 potVal = analogRead(potPin);
8 if (potVal >= 0 && potVal <= 512){
9 //do something
10 } else if (potVal >= 513 && potVal <= 768){
11 //do something different
12 } else {
13 //do something different... different
14 }
15 }
Explanation
The first variable declared on line 1 is used as an "empty" placeholder that we "fill" with a zero. We call it "potVal" in this example, but it could be anything. The constant "potPin" declared on line 2 is the pin we will wire our potentiometer's wiper up to. This could be any analog pin on the arduino, but we've chosen A0 here as an example. We use a constant so that we can reference it easy later on.
SETUP
On line 4, we initialize pin A0 on the Arduino as an input, calling the constant we just declared.
LOOP
On line 7, in the loop function, we change the value of the variable "potVal" to whatever we are reading on pin A0, which could anywhere from 0 to 1023, depending on the location of the dial. On line 8, we use a conditional statement (IF) with two conditions (separated by "&&" - meaning "and") to check if the value on the potentiometer pin is more than 1 but less than 511 -- essentially in between 0 and 512. If that returns TRUE, then line 9 is executed, otherwise it checks the next conditional on line 10. Line 10 checks to see if the value is more than 512 but less than 768. If potVal is within this range, line 11 is run. Otherwise, if none of the above conditionals return true, line 13 is run.
Explanation
BE VERY CAREFUL WHEN WIRING, ENSURE THE FLAT SIDE IS ORIENTED SUCH THAT THE 5V LINE IS ON THE LEFT. A temperature sensor is a special kind of resistor than changes resistance values based on the temperature. The higher the temperature, the less resistance, and such more voltage is detected on the green wire. The result is a range of values from 0V to 5V that can be detected by the Arduino using an analogRead(). The arduino reports this value as a 10-bit integer from 0 to 1023, 0 meaning 0V and 1023 meaning 5V. The TMP35 sensor reads temperatures from 10°C to 125°C. The TMP36 is specified from −40°C to +125°C, provides a 750 mV output at 25°C. You will need to calibrate your device regardless by measuring the ambient temperature in the room and comparing it with the value detected by the Arduino. Find your sensor value using Serial.print (see the section at the bottom of this page)
1 int tmpVal = 0;
2 const int tmpPin = A0;
3 const int roomTmp = 252; //calibration
4 void setup(){
5 pinMode(tmpPin, INPUT);
6 }
7 void loop(){
9 tmpVal = analogRead(tmpPin);
10 if (tmpVal >= 0 && tmpVal <= roomTmp){
11 //do something
12 } else {
13 //do something different
14 }
15 }
Explanation
The first variable declared on line 1 is used as an "empty" placeholder that we "fill" with a zero. We call it "tmpVal" in this example, but it could be anything. The constant "tmpPin" declared on line 2 is the pin we will wire the base pin of the sensor to. This could be any analog pin on the arduino, but we've chosen A0 here as an example. We use a constant so that we can reference it easy later on. On line 3, we add a calibration value. We should read the value of the sensor pin (see the section on Serial monitor at the bottom of this page) the first time we power it on to figure out what value is associated with the temperature in the room, which you should be able to determine with a thermometer in person. In TinkerCAD, you can click on the TMP sensor and directly set a temperature. The value you receive from Serial.println(tmpSensor); can be set to the roomTmp const.
SETUP
On line 4, we initialize pin A0 on the Arduino as an input, calling the constant we just declared.
LOOP
On line 7, in the loop function, we change the value of the variable "tmpVal" to whatever we are reading on pin A0, which could anywhere from 0 to 1023, depending on the temperature of the sesnsor. On line 8, we use a conditional statement (IF) with two conditions (separated by "&&" - meaning "and") to check if tmpVal is more than 1 but less than whatever we assigned the roomTmp to be earlier (in this example, it's 252). If that returns TRUE, then line 9 is executed, otherwise it runs the code on line 13.
Explanation
A photoresistor works very similar to a temperature sensor. The more light the resistor is exposed to, the less resistance, and such more voltage is detected on the green wire. The result is a range of values from 0V to 5V that can be detected by the Arduino using an analogRead(). The arduino reports this value as a 10-bit integer from 0 to 1023, 0 meaning 0V and 1023 meaning 5V. You will want to calibrate the device by assessing the darkest value the photoresistor produces and comparing it with the lightest value. You can achieve this with a Serial.print (read more at the bottom of this page).
1 int photoVal = 0;
2 const int photoPin = A0;
3 const int darkVal = 103; //calibration
4 const int lightVal = 952; //calibration
5 void setup(){
6 pinMode(tmpPin, INPUT);
7 }
8 void loop(){
9 photoVal = analogRead(photoPin);
10 if (photoVal >= darkVal && photoVal <= 500){
11 //do something
12 } else if (photoVal >= 501){
13 //do something different
14 } else {
15 //do nothing
16 }
17 }
Explanation
The first variable declared on line 1 is used as an "empty" placeholder that we "fill" with a zero. We call it "photoVal" in this example, but it could be anything. The constant "photoPin" declared on line 2 is the pin we will wire our photoresistor's green wire to. This could be any analog pin on the arduino, but we've chosen A0 here as an example. We use a constant so that we can reference it easy later on. On lines 3 and 4, we have placeholders for calibration values. We want to determine the range of values that should be considered "dark" vs. "bright". You will want to run a Serial.print(photoVal) first to determine these numbers (more info at the bottom of this page.
SETUP
On line 4, we initialize pin A0 on the Arduino as an input, calling the constant we just declared.
LOOP
On line 7, in the loop function, we change the value of the variable "photoVal" to whatever we are reading on pin A0, which could anywhere from 0 to 1023, depending on how bright it is. On line 8, we use two conditional statements to see if the light is brighter than what we are expecting to be the darkest value but still darker than "500". If it is within that range, it will run the code on line 11. If not, it checks the conditional on line 12 (is it greater than 501?) and if it is, runs the code on line 13. If the photoVal meets none of these conditions (i.e. it is smaller than darkVal), then it will run line 15.
Explanation
An LED is a very efficient light that uses direct current (DC). Generally, the current coming from an Arduino or other microcontroller is too much for an LED, so we always need to attach a resistor (to either leg) of the LED to limit the flow of current. 330 to 1K Ohms generally work. In this example, we're wiring the anode (long leg, red wire) directly to the Arduino digital pin, but a breadboard could be used as an intermediate.
1 const int LEDpin = 4;
2
3 void setup() {
4 pinMode(LEDpin, OUTPUT);
5 }
6
7 void loop() {
8 digitalWrite(LEDpin, HIGH);
9 delay(500);
10 digitalWrite(LEDpin, LOW);
11 delay(500);
12 }
Explanation
The first line references the digital pin we've plugged the LED circuit into, allowing us to quickly swap out this value if we wish to switch it later down the line.
SETUP
Line 4 declares the LEDpin (4) as an output. In the loop() function, line 8 sends electricity down pin 4, which completes the circuit with the LED, and thus turns it on.
LOOP
On line 9, we ask the arduino to pause instruction for half a second (500 milliseconds). On line 10, we stop sending electricity down the pin, which causes the LED to power off. Line 11 waits for another 500 milliseconds and then we start the whole loop over again, giving the appearance of the light flashing on and off every second.
Explanation
A piezo is a fancy buzzer that can play different tones/sounds depending on the digital pulse sent to it. The piezo utilizes a very special kind of digital outpit, called Pulse Width Modulation (PWM). These pins are denoted by a "~" on each pin that supports it. PWM sends a signal that changes very quickly over time in pulses, which when averaged out over time, can replicate analog values. This means that the piezo can have states in between HIGH (1) and LOW (0), which results in different tones/sounds that are emitted from the device. This is achieved using the tone() function on the Arduino instead of digitalWrite(). The tone function outputs any frequency from 20 to 20,000 Hz.
1 const int piezoPin = 6; //needs to be PWM
2
3 void setup() {
4 pinMode(piezoPin, OUTPUT);
5 }
6
7 void loop() {
8 tone(piezoPin, 200); // a low note
9 tone(piezoPin, 300); // a mid note
10 tone(piezoPin, 400); // a high note
11 }
Explanation
The first line references the digital pin we've wired the piezo to. Note that this pin MUST be a PWM (~) pin, shown on the Arduino.
SETUP
We can treat the piezo pin just like we do an LED
LOOP
Lines 8, 9 and 10 all utilize the tone() function, a special kind of Write function that sends a modulating pulse down the pin, resulting in different frequencies. You'll want to experiment with different tones for your own project. The function supports values as low as 20 and as high as 20,000, but you may not be able to hear them.
Explanation
A DC motor is a little tricky to wire because it works with a high amount of current. We have to wire a safety circuit using a diode and a PNP Transistor to ensure current only travels one way and does not overload the Arduino, and to filter out low voltages (meaning the motor should not be spinning if the Arduino sends "LOW")
Be sure to use a PNP transistor and not an NPN transistor.
1 const int motorPin = 3; //any digital pin
2 void setup(){
3 pinMode(motorPin,OUTPUT);
4 }
5
6 void loop(){
7 digitalWrite(motorPin,HIGH);
8 delay(500);
9 digitalWrite(motorPin,LOW);
10 delay(500);
11 }
Explanation
The first line of code assigns a constant to the pin we are wiring the DC motor transistor base to.
SETUP
We can treat the motor pin just like we do for the LED
LOOP
We can use a simple digitalWrite to start spinning the motor (line number 7) and again to stop the motor from spinning by setting the pin to low (line number 9).
Explanation
A servo motor is a special kind of motor that can can move to a particular angle, instead of spinning a full 360 degrees. This is useful for moving something in a very specific, particular direction. Note that there are a lot of different design standards for the colours of the wires on servo motors, red is almost always your positive (Vcc/+) supply, but your other 2 may vary and it is a good idea to consult a wiring diagram online. The servo, like the piezo, utilizes PWM to access non-binary values for the angle. The Servo.write() function accepts an angle, generally in between 0 and 180 for most micro servos, and sends it to the signal pin of the servo (shown in orange here)
1 #include <Servo.h>
2 Servo myServo; //importing a package
3 const int servoPin = 5; //needs to be PWM
4 void setup() {
5 myServo.attach(servoPin);
6 }
7
8 void loop() {
9 myServo.write(10); //10 degrees
10 delay(2000);
11 myServo.write(170); //170 degrees
12 delay(2000);
13 }
Explanation
The first two lines of code imports a special package needed to use a servo without too many extra lines of code. This package, called "Servo", will use other code from another file to control the motor.
SETUP
On line 5, instead of using a pinMode setup, we use a special Servo function to tell the Arduino where to find the signal pin. In the loop function, we use a special kind of digitalWrite to send a PWM signal to the servo with the angle we want.
LOOP
On line 9, we tell the servo to rotate to 10 degrees, which is basically as far counterclockwise as it will go. We wait for two seconds using the delay(2000) and then on line 11 we ask the servo to rotate to 170 degrees which is basically as far clockwise as the motor can go. This repeats over and over, flipping back and forth every 4 seconds.
Explanation
An LCD screen is a complex output device composed of 2 rows of 16 blocks (32 blocks) which can each display an array of 5x8 1-bit pixels, for a grand total of 1280 pixels on the display. You may be thinking, how can I turn on and off 1280 different pixels with only a dozen or so wires? Well, the LCD screen can save a little bit of data in it's on-board memory and accepts data from the arduino one byte at a time through the DB4, DB5, DB6, and DB7 pins (shown in green, light blue, blue, and purple wires). The yellow and orange wires allow the Arduino to tell where on the screen to draw the next character. This position is called the "cursor" (note the coordinates for each cursor location below)
1 #include <LiquidCrystal.h>
2 LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
3
4 byte customChar[] = {
5 B11011, // ██ ██ a smiley face
6 B11011, // ██ ██ made using customChar
7 B11011, // ██ ██
8 B00000, //
9 B10001, // █ █ notice the bits 1 and 0
10 B11011, // ██ ██ 1 is on, 0 is off
11 B01110, // ███
12 B00100 // █
13 };
14
15 void setup() {
16 lcd.begin(16, 2);
17 lcd.createChar(0, customChar);
18 }
19 void loop() {
20 lcd.setCursor(0,0);
21 lcd.print("Hello!");
22 lcd.setCursor(0,1);
23 lcd.write((int)0); // the smile
24 delay(5000);
25 lcd.clear();
26 delay(1000);
27 }
Explanation
The first two lines of code imports a special package to use the LCD screen. The second line of code declares the pins that the Arduino will use to send bytes of information to the LCD display. The first two pins (in this example, 12 and 11) are the write enable and register select pins, and the last 4 pins (5, 4, 3, 2) are the data entry pins. These pins can be switched around but you will need to change these numbers in the code to reflect your changes.
The LCD can show any ASCII character using the function lcd.print("YOUR TEXT HERE"), make sure your text is in quotations, as you can see on line 21. If you want a custom character that isn't in ASCII, you can create one. On line 4, a custom character is stored in a byte variable. You can make the character any combination of 5 pixels wide by 7 pixels tall. You can see in the comments what this custom character is supposed to look like, a smiley face, and the final result as it is seen on the LCD on the left here. Pay close attention to the numbers on lines 5 through 12, they show you which pixels light up and which pixels remain unlit.
The setCursor() function changes where you can write your next character. Think of it like the blinking vertical line in Google Docs when you are typing, only it is invisible on the LCD. setCursor() takes an (x,y) coordinate, where (0,0) is the top left corner and (15,1) is the bottom right corner of the display.
The lcd.clear() function removes everything on the screen when called.
Explanation
The Arduino can communicate with your PC over the USB cable you are using to upload code and power the device. In the IDE (code editor) simply, open up the "Serial Monitor" found in the top right corner on the desktop IDE and in the bottom of the code editor on TinkerCAD
1 void setup(){
2 Serial.begin(9600);
3 }
4 void loop(){
5 Serial.print("Hello!");
6 Serial.println(" New Line after this!);
7 }
Explanation
The serial monitor is a very simple output to help you debug and troubleshoot aspects of your code. To initialize it (i.e. to tell your Arduino you want to listen to it on the PC), use the Serial.begin() function (note the capital "S"). We transmit the signal at 9600 baud, which essentially means 9600 bits per second. The computer will listen at that frequency to ensure no other signals get accidentally interpreted as part of your message.
To send something to the serial monitor, you have two options, Serial.print(), which sends whatever you want in the brackets to the serial monitor, and Serial.println(), which does the same thing but adds a line break after your message.
You can print any plain text ASCII by putting it in quotation marks, but you can also print any variables and constants for debugging purposes.
For example, it's a really good idea to use
Serial.println(switchState);
to check if your switch is actually being pressed. This will print a 0 or 1 if you've done a digitalRead with the variable switchState.