Arduino: Serial Monitor Diagrams & Code: Project 01: Monitor How Much Light Is Hitting A Photoresistor
Arduino: Serial Monitor Diagrams & Code: Project 01: Monitor How Much Light Is Hitting A Photoresistor
All projects require the use of the serial monitor in your Arduino IDE program (or whatever you are using to
transfer code to the Arduino).
void setup() {
Serial.begin(9600); // initialize the serial communication
// Note: analog pins are automatically set as inputs
}
void loop() {
sensorVal = analogRead(sensorPin); // read the value from the sensor
delay(1000);
}
3/2018
Brown County Library
Project 02: Monitor the temperature
Components needed:
Arduino Uno board
breadboard
TMP 36 temperature sensor
3 jumper wires
Warning: Temperature sensors can heat up quickly if they are not hooked up correctly! In the diagram
below, the flat side of the temperature sensor should be facing the Arduino. For more
information, go here: https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-experiment-
guide---v40/circuit-4b-temperature-sensor
3/2018
Brown County Library
/*
Serial Monitor 02 : Monitor the temperature
Code partially adapted from the Arduino Projects Book (2012)
*/
void setup() {
Serial.begin(9600); // initialize the serial communication
// Note: analog pins are automatically set as inputs
}
void loop() {
sensorVal = analogRead(sensorPin); // read the value from the sensor
Serial.print(", degrees C: "); // send the Celsius reading to the serial monitor
Serial.print(temperatureC);
Serial.print(", degrees F: "); // send the Fahrenheit reading to the serial monitor
Serial.println(temperatureF);
delay(1000);
}
3/2018
Brown County Library
Project 03: Lighting up LEDs based on the temperature
Components needed:
Arduino Uno board
breadboard
TMP 36 temperature sensor
7 jumper wires
3 x 220 ohm resistors
3 x LEDs
Warning: Temperature sensors can heat up quickly if they are not hooked up correctly! In the diagram
below, the flat side of the temperature sensor should be facing the Arduino. For more
information, go here: https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-experiment-
guide---v40/circuit-4b-temperature-sensor
3/2018
Brown County Library
/*
Serial Monitor 03 : Lighting up LEDs based on the temperature
Code partially adapted from the Arduino Projects Book (2012)
*/
void setup() {
Serial.begin(9600); // initialize the serial communication
// Note: analog pins are automatically set as inputs
for(int pinNumber = 2; pinNumber<5; pinNumber++){ // pinNumber 2 and above but less than 5...
pinMode(pinNumber,OUTPUT); // LEDs are set as outputs and...
digitalWrite(pinNumber, LOW); // are turned off
}
}
void loop() {
sensorVal = analogRead(sensorPin); // read the value from the sensor
Serial.print(", degrees C: "); // send the Celsius reading to the serial monitor
Serial.print(temperatureC);
Serial.print(", degrees F: "); // send the Fahrenheit reading to the serial monitor
Serial.println(temperatureF);
}else if(temperatureF >= baselineTemp+1 && // if the temperature is more than 1 degree F but...
temperatureF < baselineTemp+2){ // less than 2 degrees above the baseline temp...
digitalWrite(2, HIGH); // the first LED is on and the others are off
digitalWrite(3, LOW);
digitalWrite(4, LOW);
3/2018
Brown County Library
}else if(temperatureF >= baselineTemp+2 && // if the temperature is more than 2 degrees F but...
temperatureF < baselineTemp+3){ // less than 3 degrees above the baseline temp...
digitalWrite(2, HIGH); // the first 2 LEDs are on and the last is off
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
}else if(temperatureF >= baselineTemp+3){ // if the temperature is more than 3 degrees F above the baseline temp...
digitalWrite(2, HIGH); // all of the LEDs are on
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
delay(1000);
}
3/2018
Brown County Library
Project 04: Receive data from a computer
Components needed:
Arduino Uno board
breadboard
3 jumper wires
2 x 220 ohm resistors
2 x LEDs
Once you complete the setup and transfer the code, open the serial monitor and enter any number between
one and four.
3/2018
Brown County Library
/*
Serial Monitor 04 : Receive data or a command from a computer
Code partially adapted from Physical Pixel Tutorial (arduino.cc/en/Tutorial/PhysicalPixel)
and Adafruit Arduino Lesson 5 (learn.adafruit.com/adafruit-arduino-lesson-5-the-serial-
monitor)
*/
void setup() {
Serial.begin(9600); // initialize the serial communication
pinMode(redPin, OUTPUT); // red pin is an output
pinMode(greenPin, OUTPUT); // green pin is an output
}
void loop() {
// Check for incoming serial data
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
3/2018
Brown County Library
Project 05: Control a RGB LED with the serial monitor
Components needed:
Arduino Uno board
breadboard
4 jumper wires
3 x 220 ohm resistors
Common cathode RGB LED
o If you have a common anode RGB LED, connect the long pin to 5V instead of ground, and use
the code found on page 12
Once you complete the setup and transfer the code, open the serial monitor and enter RGB color codes to set
the LED to a specific color. For example, to turn on just the red light, enter: 255,0,0
Depending on your serial monitor, you may need to choose “new line” from a dropdown menu.
Remember that some colors show up better than others. Consult a RGB Color Picker for color combinations,
such as: webpagefx.com/web-design/color-picker/
3/2018
Brown County Library
/*
Serial Monitor 05 : Control a RGB LED with the serial monitor
Source: Code modified from Arduino.cc Read ASCII Tutorial
(arduino.cc/en/Tutorial/ReadASCIIString)
For use with a common cathode RGB LED
*/
void setup()
{
Serial.begin(9600); // initialize the serial communication
void loop()
{
while (Serial.available() > 0) // check for incoming serial data
{
// look for 3 valid integers (numbers) separated by commas - this will set the RGB values (ex. R,G,B)
int red = Serial.parseInt(); // first valid integer
int green = Serial.parseInt(); // second valid integer
int blue = Serial.parseInt(); // third valid integer
3/2018
Brown County Library
/*
Serial Monitor 05 : Control a RGB LED with the serial monitor
Source: Code modified from Arduino.cc Read ASCII Tutorial
(arduino.cc/en/Tutorial/ReadASCIIString)
For use with a common anode RGB LED
*/
void setup()
{
Serial.begin(9600); // initialize the serial communication
void loop()
{
while (Serial.available() > 0) // check for incoming serial data
{
// look for 3 valid integers (numbers) separated by commas - this will set the RGB values (ex. R,G,B)
int red = Serial.parseInt(); // first valid integer
int green = Serial.parseInt(); // second valid integer
int blue = Serial.parseInt(); // third valid integer
3/2018
Brown County Library
Ideas to Build On
What happens when the baud rates don’t match between the code and the serial monitor?
Learn More
Want to learn more about how the serial monitor works? Try these resources:
3/2018
Brown County Library