Lab: Digital Input, Output and Analog Input with an Arduino
Digital Input & Output with an Arduino
Arduino Uno connected to pushbutton and LED
10k ohm resistor in line 15 beside pushbutton to complete circuit(prevent short circuit!) also connected in ‘–‘
junction point : between pushbutton and 10k ohm
and adding two LEDs (Red, Yellow)…
void setup() { pinMode(2, INPUT); // set the pushbutton pin to be an input pinMode(3, OUTPUT); // set the yellow LED pin to be an output pinMode(4, OUTPUT); // set the red LED pin to be an output } void loop() { // read the pushbutton input: if (digitalRead(2) == HIGH) { // if the pushbutton is closed: digitalWrite(3, HIGH); // turn on the yellow LED digitalWrite(4, LOW); // turn off the red LED } else { // if the switch is open: digitalWrite(3, LOW); // turn off the yellow LED digitalWrite(4, HIGH); // turn on the red LED } }
Lab: Analog In with an Arduino
Add a Potentiometer and LED
const int ledPin = 9; // pin that the LED is attached to int analogValue = 0; // value read from the pot int brightness = 0; // PWM pin that the LED is on. void setup(){ // initialize serial communications at 9600 bps: Serial.begin(9600); // declare the led pin as an output: pinMode(ledPin, OUTPUT); } void loop() { analogValue = analogRead(A0); // read the pot value brightness = analogValue /4; //divide by 4 to fit in a byte analogWrite(ledPin, brightness); // PWM the LED with the brightness value Serial.println(brightness); // print the brightness value back to the serial monitor }
a potentiometer connected to analog in 0 of an Arduino and an LED connected to digital pin 9.
const int pinNumber = 9; // pin that the LED is attached to int analogValue = 0; // value read from the pot int frequency = 0; // PWM pin that the LED is on. void setup(){ // initialize serial communications at 9600 bps: Serial.begin(9600); // declare the led pin as an output: pinMode(pinNumber, OUTPUT); } void loop() { analogValue = analogRead(A0); // read the pot value frequency = (analogValue /4) * 10; // divide by 4 to fit in a byte, multiply by 10 for a good tonal range tone(pinNumber, frequency); // make a changing tone on the speaker }
replacing the LED with a speaker that will play a changing tone.
Gear motor + potentiometer + pushbutton control
To further, I wanted to control DC motor by adjusting the intensity with a potentiometer on the pushbutton. So in this page, ‘Lab: Using a Transistor to Control High Current Loads with an Arduino’s Figure 22, I experimented with motor before connecting to my labtop and arduino first. It looks like working well.
Things you need
- microcontroller
- jumper wire/ aligator clips
- gear motor
- potentiometer
- breadboard
- 1N400x power diode
- TIP120 transistor

DC motor with potentiometer, before connecting with laptop, programming
DC motor with pushbutton before connecting laptop, programming.
lt seemed working well..
an Arduino connected to a potentiometer, a transistor, a DC motor, and a DC jack. A transistor is connected to Digital Pin 9. A DC motor connects to the transistor and a DC jack. The DC jack connects its positive wire to the first wire of the DC motor. The negative wire of the DC jack connects to ground. The second wire of the DC motor connects to the collector of the transistor. A 1N400x diode’s cathode is connected to the collector, and its anode is connected to ground.

In this circuit, I added one pushbutton here.
Now, connect all together, Potentiometer + pushbutton + gear motor(with a pinwheel) In other words, control a gear motor by adjusting the intensity with a potentiometer on the pushbutton’s on & off.
- on= push (which is ‘move’ the motor)
- off = not push (which is ‘stop’ the motor)

const int transistorPin = 9; const int button = 2; int buttonValue = 0; int Pot = A0; void setup() { Serial.begin(9600); pinMode(transistorPin, OUTPUT); pinMode(button, INPUT); pinMode(Pot, INPUT); } void loop() { buttonValue = digitalRead(button); if (buttonValue == HIGH) { int sensorValue = analogRead(A0); // map the sensor value to a range from 0 - 255: int outputValue = map(sensorValue, 0, 1023, 0, 255); analogWrite(transistorPin, outputValue); } else { analogWrite(transistorPin, 0); } }
It seemed that the intensity with a potentiometer worked well but the pushbutton did not work.
Problem & Questions
– Why the pushbutton did not work? not a complete circuit? or code error in code sketch?
– why a 1N400x power diode needed?
– alternatives? motor drive or H-bridge??