# Beginning Arduino/Circuits - Middle School Age Appropriate

I created a sketch to light a red/green LED to indicate a voltage from a potentiometer. It would be appropriate as a middle school project, although I have not written up a lesson plan as of yet. Includes sketch, ReadMe, schematic, pictures, etc. Uses basic division and multiplication, and an if/else operator. I'm learning too, and would love any feedback you would like to share. I feel like there's lots of potential to scale this up or down, too. Uses Arduino Uno. Check it out:

I would suggest you included a flowchart. I have a very graphical mind, I can pickup the concept shown by a flowchart very quickly compared to a textual presentation. It might help some in your class.

I also think you might consider a time related red/green. I don't have any idea how the voltage on a pot would be comprehended by a middle school student. But I know the know time.

Maybe a simulated Red/Green light. You could use a button to simulate the vehicle sensor in the road.

Nice presentation, can you post your code here in the forum please.
The cloud will not allow to read pdf files.

I like your circuit diagrams better than Fritzy images and along with the jpg images should go down well.

Tom...

Here's the code to try it if you want @TomGeorge . Thanks for the kudos I am still new to the game. Is there a way to edit the original post to include the code there?

``````/*
Date: Sept 10, 2021
Revision: Version 0.2.0 Beta

Project: Arduino UNO #1

Remember, "*" means to multiply, "/" means to divide
To convert analog voltage input on A0 to analog output -> 255/513 * voltage or 0.49708 * voltage. I used 513 instead of 512 to avoid
a rounding error at voltage 513 and 514, which caused unexpected behavior from the light. This is a result of debugging, because half
of 1023 is 511.5, which rounds to 512.

255 is max Pulse Width Modulation, 512 is perfect voltage. It equates to 2.5 volts, or equal ratios of resistance in the pot.

It still indicates 512 as perfect. However, it looks green from about 507 to 517 or so, or 2.48v to 2.53v.
NOTE: It is more complicated to use a number other than 512 as perfect, since it creates two ratios that must be used. Try it!
*/

#define BAD 9 // Set red output - may need to switch LED or numbering within program
#define GOOD 10 // Set green output - #define does not use up memory resources on the Uno
#define V_STATUS 0 // Set voltage input pin (A0)

void setup() {

pinMode(BAD, OUTPUT); //Set as outputs for LEDs
pinMode(GOOD, OUTPUT);
analogWrite(GOOD, LOW); // Initializes red/green LED to off (LOW) (0)

}

void loop() {

int pwmOut = 0.49708 * voltage; //int is a whole number variable type so it will automatically round to a whole number
// Also resets pwmOut every time

if (voltage <= 512) { // 512 is seen here as the ideal voltage (2.5V) when using the 5V out pin
analogWrite(GOOD, pwmOut);
analogWrite(BAD, (255 - pwmOut)); // From 0 to 512 its easy to convert the linear change
}

else {
pwmOut = pwmOut - 255; // This adjusts pwmOut, since above 513 voltage, pwmOut is greater than 255
analogWrite(GOOD, (255 - pwmOut)); // 513 to 1023, it is a inverse relationship between increasing voltage and PWM
}

delay(3); // delay to view LED change

}
``````

@JohnRob I think the idea of a flowchart is a great idea! Thanks, I am much more wordy. And my wife always complains my ideas are college level, so I will definitely consider a time-related sketch. Thanks for the input.

@JohnRob What do you think of this?

IMHO I think you are still at college (or at least high school) level, and needless blocks.

I would:

First chart:

1. draw the pot and show the wiper going into a block with the text voltage to digital conversion. Having the block called "Process" is too diversionary.

2. If statement:
if voltage > 3
true = turn on red light and turn off green
false = turn on Green light and turn off red

Loop to the beginning.

Second chart:

The below is one I did for myself to make sure I had the best approach. Maybe it will help.

I don't know your wife's knowledge for this type of design. But she may be valuable in trying to see what your are saying without any pre explanation.

debounce flowchart (lvl3).pdf (13.3 KB)

@JohnRob Thanks for the chart and the info. I'll keep at it. I showed the chart to my wife before you replied and she agreed with you completely. Back to the drawing board it is!

I've learned to keep them as simple minimum of words without needing to use Jargon.

Things like Red_On is immediately comprehended by almost anyone.

BTW, early in my career I had to write many test procedures. I learned a lot by the questions raised from my procedures. In addition, some of the reports went to other countries where English is not the 1st language.

@JohnRob I appreciate your help a lot. I figure I'm never too old to stop learning!

I made some changes in your code. You may or may not think they are good changes.

``````/*
Date: Sept 10, 2021
Revision: Version 0.2.0 Beta

Project: Arduino UNO #1

Remember, "*" means to multiply, "/" means to divide

The Arduino Uno analog to digital converter compares an analog input (i.e. A0) to the 5v supply voltage.
It divides the 5V into 1024 steps called "counts" (0 to 1023).  It compares the A0 voltage to the different steps and
returns the count of the step that most closely equals the input voltage on A0
Each step is 5V/1024 = 0.00488 volts.

The PWM (Pulse Width Modulated) outputs a 490 Hz (aka 490 times a second) waveform.  The on time can be controlled in 512 steps
0 = not on time
256 = on 50% of the time
512 = on 100% of the time

In the code below, the processor will "measure" the voltage (by converting it to counts, see above)

At very low counts, the RED light will be dominant and the GREEN off.  As the voltage on the pot increases,
the RED will start to dim and the GREEN will increase in brightness.
When the pot is in the center, both LED will be on the same brightness.
etc

*/

#define RED 9 // Set red output - may need to switch LED or numbering within program
#define GREEN 10 // Set green output - #define does not use up memory resources on the Uno
#define V_POT 0 // Set voltage input pin (A0)  I know middle school will snicker at "POT"

void setup() {

pinMode(RED, OUTPUT); //Set as outputs for LEDs
pinMode(GREEN, OUTPUT);
analogWrite(RED, 0); // Initializes red/green LED to off (LOW) (0)
analogWrite(GREEN, 0);

}

void loop() {

int pwmOut = 0.50 * InputVoltage; //int is a whole number variable type so it will automatically round to a whole number
// Also resets pwmOut every time

analogWrite(GREEN, (255 - pwmOut)); // From 0 to 512 its easy to convert the linear change
analogWrite(RED, pwmOut);

delay(100); // delay in milliSeconds. Makes the program respond more slowly to changes in POT voltage.

}
``````

@JohnRob I think this would be great. I plugged it into the circuit and refined it a bit, and then put our names on it. With your permission I will put together another sketch with the additional circuit schematic, etc, and make it public. If you would rather not, no problem too.

``````/*
Date: Sept 11, 2021
Revision: Version 0.3.0 Beta

Project: Arduino UNO #1

The Arduino Uno analog to digital converter compares an analog input (i.e. A0) to the 5v supply voltage.
It divides the 5V into 1024 steps called "counts" (0 to 1023).  It compares the A0 voltage to the different steps and
returns the count of the step that most closely equals the input voltage on A0
Each step is 5V/1024 = 0.00488 volts.

The PWM (Pulse Width Modulated) outputs a 490 Hz (aka 490 times a second) waveform.  The on time can be controlled in 255 steps
0 = not on time
128 = on 50% of the time
255 = on 100% of the time

In the code below, the processor will "measure" the voltage (by converting it to counts, see above)

At very low counts, the RED light will be dominant and the GREEN off.  As the voltage on the pot increases,
the RED will start to dim and the GREEN will increase in brightness.
When the pot is in the center, both LED will be on the same brightness, creating an orange color.

*/

#define RED 9 // Set red output - may need to switch LED or numbering within program
#define GREEN 10 // Set green output - #define does not use up memory resources on the Uno
#define V_POT 0 // Set voltage input pin (A0)

void setup() {

pinMode(RED, OUTPUT); //Set as outputs for LEDs
pinMode(GREEN, OUTPUT);
analogWrite(RED, 0); // Initializes red/green LED to off (LOW) (0)
analogWrite(GREEN, 0);
Serial.begin(9600);
}

void loop() {

int pwmOut = 0.25 * InputVoltage; //int is a whole number variable type so it will automatically round to a whole number

analogWrite(RED, (255 - pwmOut)); // When pwmOut is at max, RED is at zero
analogWrite(GREEN, pwmOut); // When pwmOut is at max, GREEN is at full brightness (255)
Serial.println(pwmOut);

delay(100); // delay in milliSeconds. Makes the program respond more slowly to changes in POT voltage.

}
``````

I think the change from BAD and GOOD to RED and GREEN was very good. It goes right along with what you were saying earlier. Also I'm glad you added the parts about voltage steps and PWM. You were right, I have learned a lot from input about the sketch!

Be my guest. Can do it with or without my name. I'm just happy to help.

(post deleted by author)

Contains schematic, photos, sketch and more to help grasp the project quickly and easily. Please let me know your thoughts!!

Basic sketch outline

Schematic

View the sketch here:

Here's the code:

``````/*
Date: Sept 18, 2021
Revision: Version 0.1.2 Beta

Project: Arduino Uno no3

The Arduino Uno analog to digital converter compares an analog input (i.e. A0) to the voltage at the wiper of the potentiometer
(maximum 5 volts, because it is using the 5v pin). A potentiometer is a device that creates a variable voltage.

It divides the 5V into 1024 levels called "counts" (0 to 1023).  It compares the A0 voltage to the different levels and
returns the count of the level that most closely equals the input voltage on A0
Each level is 5V/1024 = 0.00488 volts.

The PWM (Pulse Width Modulated) outputs a 490 Hz (aka 490 times a second) waveform.  The on time can be controlled in 255 steps
0 = not on any time
128 = on 50% of the time
255 = on 100% of the time

In the code below, the processor will "measure" the voltage (by converting it to counts, see above)

At very low counts, the RED light will be dominant and the GREEN off.  As the voltage on the pot wiper increases,
the RED will start to dim and the GREEN will increase in brightness.
When the pot is in the center, both LED will be on the same brightness, creating an orange color.

*/

#define RED 9 // Set red output - may need to switch LED or numbering within program
#define GREEN 10 // Set green output - #define does not use up memory resources on the Uno
#define V_POT 0 // Set voltage input pin (A0)

void setup() {

pinMode(RED, OUTPUT); //Set as outputs for LED
pinMode(GREEN, OUTPUT);
analogWrite(RED, 0); // Initializes red/green LED to off (LOW) (0)
analogWrite(GREEN, 0);
}

void loop() {

int inputVoltage = analogRead(V_POT); // Read voltage at pin A0 and store it in a variable called inputVoltage

int pwmOut = 0.25 * inputVoltage; // Change voltage counts into Pulse Width Modulation steps and store in a variable called pwmOut.
// int is a whole number variable type so it will automatically round to a whole number

analogWrite(RED, (255 - pwmOut)); // When pwmOut is at max, RED is at 0
analogWrite(GREEN, pwmOut); // When pwmOut is at max, GREEN is at full brightness (255)

delay(100); // delay in milliSeconds. Makes the program more stable when responding to changes in POT voltage.
// To get seconds, divide by 1000 or move the decimal left 3 places.  100.0 ms = 0.1 seconds

}
``````

Looks good so far.
Of course, all the detail will be in "Change voltage counts into PWM steps".
0-511 will control brightness of one color, 512-1023 will control brightness of the other? (keeping in mind that analogWrite() only takes 0-255 as a value)
Maybe something different so both can be on? Maybe 0-255 is only one fading up, 255-511 is onlythe other fading up, 512- 755 is both fading up, 756-1023 is both fading down?
Lots of options.

Why did you start a new topic for this?
https://forum.arduino.cc/t/beginning-arduino-circuits-middle-school-age-appropriate/904312