Arduino Basic Experiment Tutorial

The following experiments are designed to help students initially master how to use microcontrollers and achieve some interesting functions with basic circuit components.

The microcontroller control module selected is Arduino UNO R3, which integrates the development environment Arduino IDE that encapsulates many complex low-level operations into functions for beginners to call, greatly lowering the learning threshold and simplifying programming workload, making it very suitable for zero-based entry-level learning.

Arduino Basic Experiment Tutorial

Arduino UNO R3 Development Board

Note: When developing in the Arduino environment, be sure to make good use of the serial monitor, which is a good tool.

Related resource links:

Fire Spark Space http://www.oursparkspace.cn/

Arduino Official Website https://www.arduino.cc/

Open Source Software Hosting Platform https://github.com/

In addition, everyone must get used to solving problems by themselves and using search engines to find answers.

Arduino Basic Experiment Tutorial

Many things are like traveling; when you decide to set off, the most difficult part has already been completed.

Basic Experiments

1. Automatic Control of Street Lights

Requirements: The street lights should turn off during the day when the light is strong; they should turn on at night when the light is weak.

Design Idea: Use a light-sensitive component to sense the light intensity and control the street lights through mCookie.

Arduino Basic Experiment Tutorial

Principle and Wiring Diagram of Automatic Control of Street Lights

Note: Connect the circuit according to the left schematic diagram! Do not follow the physical diagram.

Street lights are replaced with light-emitting diodes, and the light-emitting diodes must be connected in series with a current-limiting resistor.

Reference code:

int threshold =400; //Light intensity value, adjust according to actual conditions

void setup ( )

{

Serial.begin(115200);

pinMode(10, OUTPUT); //Set output port

}

void loop( )

{

int n = analogRead(A3); //Read analog port A3

Serial.println(n);

if (n>threshold ) //At night, the light is dark, and n value increases

digitalWrite(10, HIGH); //Turn on the street light

else

digitalWrite(10, LOW); //Turn off the street light

delay(200);

}

Note: The ports in the program must be modified according to the actual circuit connection and must correspond to the ports connected to the circuit.

Questions:

(1) How to change it to a light intensity warning circuit, where the light-emitting diode lights up when the light intensity is strong?

(2) How to make the light-emitting diode become a breathing light? (Hint: Use PWM to control the LED)

(3) How to design an automatic dimming desk lamp? (Hint: Introduce a light-sensitive resistor)

(4) How to design a colorful desk lamp? That is, control the color light-emitting diodes to display different colors of light according to different light intensities.

2. Digital Thermometer

Requirements: Use a temperature sensor to detect the ambient temperature and observe the changes in ambient temperature through the IDE’s serial monitor. The higher the ambient temperature, the brighter the light-emitting diode should be, presented to the user in an appropriate way.

Arduino Basic Experiment Tutorial

Principle and Wiring Diagram of Digital Thermometer

Note: (1) The temperature sensor used is LM35D, which looks similar to a transistor. The output is an analog voltage value, and each increase of 10mV in voltage represents an increase of 1°C in temperature, and the two are linearly related.

(2) This circuit uses an internal reference source for ADC sampling. To achieve accurate temperature measurement, the reference voltage for analog-to-digital conversion must be stable, and the REF pin of mCookie should be connected to an accurate voltage source.

(3) The physical diagram does not connect the light-emitting diode and buzzer.

Arduino Basic Experiment TutorialArduino Basic Experiment Tutorial

Reference code:

unsigned int tempMin = 29; //Lighting temperature

unsigned int tempMax = 33; //Alarm temperature

void setup( ) {

Serial.begin(115200); //Serial initialization

analogReference(INTERNAL); //Call the onboard 1.1V reference source

pinMode(11, OUTPUT);

digitalWrite(11, LOW);

}

void loop( ) {

double analogVotage = 1.1*(double)analogRead(A3)/1023;

double temp = 100*analogVotage; //Calculate temperature

unsigned int dutyCycle; //Duty cycle

if (temp <= tempMin) { //Less than the lighting threshold

dutyCycle = 0; digitalWrite(11, LOW);

}

else if (temp < tempMax) { //Less than the alarm threshold

dutyCycle = (temp-tempMin)*255/(tempMax-tempMin);

digitalWrite(11, LOW);

}

else{ //The brightness of the light-emitting diode is at its maximum, and the sound alarm is activated

dutyCycle = 255; digitalWrite(11, HIGH);

}

analogWrite(10, dutyCycle);//Control the brightness of the light-emitting diode

Serial.print(“Temp: “); Serial.print(temp);

Serial.print(” Degrees Duty cycle: “);

Serial.println(dutyCycle);

delay(100);// Control refresh speed

}

Questions:

(1) How to display the changes in ambient temperature using a color light-emitting diode?

(2) How to display the ambient temperature using a seven-segment display?

(3) How to display the ambient temperature using an OLED screen?

3. Intelligent Temperature-Controlled Fan

Requirements: Automatically adjust the fan speed according to the ambient temperature.

Arduino Basic Experiment Tutorial

Intelligent Temperature-Controlled Fan Principle and Wiring Diagram

Note: Motors generally require a larger current, and the driving capability of the microcontroller’s IO port is limited, so a transistor is used for current amplification. Since PWM is used, the transistor operates in a switch state.

Reference code:

double analogVotage; //Analog voltage value

double temp; //Temperature

unsigned int dutyCycle; //Duty cycle

unsigned int tempMin = 25; //Zero-speed temperature, set to the ambient temperature observed from the serial port

unsigned int tempMax = 35; //Full-speed temperature, set to the temperature observed from the hand-held component

void setup() {

Serial.begin(115200); //Set serial baud rate

analogReference(INTERNAL); //Call the onboard 1.1V reference source

}

void loop() {

analogVotage = 1.1*(float)analogRead(A3)/1024; //Convert ADC reading to voltage

temp = 100*analogVotage; //Convert voltage to temperature

if (temp < tempMin){

dutyCycle = 0;

}

else if (temp < tempMax){

dutyCycle = (temp-tempMin)*255/(tempMax-tempMin);

}

else {

dutyCycle = 255;

}

analogWrite(10,dutyCycle); //Generate PWM

Serial.print(“Temp: “); Serial.print(temp);

Serial.print(” Degrees Duty cycle: “);

Serial.println(dutyCycle);

delay(100); //Wait 0.1 seconds, control refresh speed

}

Questions:

(1) Why not directly drive the motor with the microcontroller’s IO port?

(2) How to make the fan start only when someone is detected and automatically adjust the speed according to the ambient temperature?

(3) Add an OLED screen to display the ambient temperature and fan speed in real-time.

Supplement: The pyroelectric infrared sensor is equipped with a Fresnel lens, which can sensitively detect human activity. Many of these sensors are available on Taobao, and you can purchase them according to your needs.

Arduino Basic Experiment Tutorial

4. Non-inverting Proportional Operation Circuit

Requirements: Use an integrated operational amplifier to achieve linear amplification of the input signal.

Arduino Basic Experiment Tutorial

Principle and Wiring Diagram of Non-inverting Proportional Operation

Note: The input can use resistors or potentiometers for voltage division. Carbon film potentiometers are very cheap on Taobao, and you can buy several for one yuan. If the input signal is too large, the output will enter saturation. The voltage values of the input and output are collected by mCookie and can be viewed through the IDE’s serial monitor.

Reference code:

void setup( ) {

Serial.begin(115200);

}

void loop( ) {

float vi=analogRead(A2)/1023.0*5;

float vo=analogRead(A3)/1023.0*5;

Serial.print(“Input voltage is “);

Serial.print(vi); Serial.println(“V”);

Serial.print(“Output voltage is “);

Serial.print(vo); Serial.println(“V”);

delay(1000);

}

//In this program, the A2 port of the Arduino development board is used to collect the input analog voltage value, and the A3 port is used to collect the output analog voltage value.

Questions:

(1) How to change the amplification factor? Can the amplification factor be very large?

(2) Theoretically, how to achieve reverse proportional amplification?

5. Touch Electronic Piano

Requirements: Play different tunes by touching different keys with your fingers.

Design Idea: Use Arduino’s readCapacitivePin() function to read the capacitance value of the port; use tone(pin, frequency) function to produce different tones.

First, do a fingertip switch experiment.

The fingertip switch, also known as a touch switch, uses the resistance of the human body as a switch to control the conduction of a transistor, thereby controlling the light-emitting diode.

Arduino Basic Experiment Tutorial

Principle Diagram of Fingertip Switch

Using a multimeter, the resistance between the fingers of both hands is approximately between 200k ohms to 2 megaohms, depending on the dryness of the skin. If the finger does not touch the switch, the base of the transistor is floating, and the emitter junction be is cut off, causing the transistor to turn off, and the voltage at point A3 is pulled down to 0V by the 10k resistor.

When the fingertips of both hands touch the switch, it is equivalent to applying a 5V voltage through a resistance of about 1M ohms to the base of the transistor, causing the transistor’s emitter junction be to conduct due to forward bias, turning on the transistor. Although the base current is small, due to the transistor’s current amplification effect, a relatively large current flows out of the emitter, creating a voltage difference across the 10k emitter resistor, making point A3 high level. When the Arduino UNO detects that the level at point A3 is high, it sets pin D13 to high level, turning on the light-emitting diode.

Reference code

void setup()

{

Serial.begin(115200);

pinMode(10,OUTPUT);

}

void loop()

{

int n=analogRead(A3); //Read analog port data

if(n>50) //If there is a voltage response, run the following program

digitalWrite(10,HIGH); //Light up LED

else

digitalWrite(10,LOW); //Turn off LED

Serial.println(n);//Serial monitor

delay(100);//Delay to control refresh rate.

}

How to Measure Capacitance Value

In addition to receiving digital signals from digital ports, Arduino can only detect the analog physical quantity, which is voltage. The detection value of any analog sensor must be converted into a voltage value through related circuits before being input into the Arduino’s analog ports for analog-to-digital conversion. The capacitance value requires a more complex circuit to convert it into a voltage value so that it can be detected by Arduino. Here is a method to detect capacitance that requires only a piece of wire and a port, without any components.

The idea of this method is to first set a digital port to a low potential and turn on the internal pull-up resistor of Arduino, then start timing how long it takes for this port to reach a high potential. This time is related to the capacitance value of this port to ground; the larger the capacitance, the longer the time. In hardware, you only need to connect a wire to a port. Touching the exposed end of this wire with your finger will cause a change in capacitance, which Arduino can detect through the above method. To increase sensitivity, you can connect a piece of tin foil to the wire.

Note: To prevent static electricity from damaging the core module of Arduino, it is recommended to touch a radiator to release human static electricity first, or cover the wire with an insulating board.

Touch Switch

We first test the touch switch to see if the capacitive value detection port is touched. If it detects that a finger touches the port lead, the LED will light up. During the experiment, the touch button is connected to pin D8, and pin D11 controls the LED.

Arduino Basic Experiment Tutorial

Reference code

int ledPin = 11; //Control LED with pin D11

int capval;

void setup()

{

pinMode(ledPin, OUTPUT);

Serial.begin(115200);

Serial.println(“Touch sensor”);

}

void loop ()

{

digitalWrite(ledPin,LOW);

capval = readCapacitivePin(8); //Read capacitance value through pin D8

Serial.println(capval, DEC);

if (capval > 2) {

// turn LED on:

digitalWrite(ledPin, HIGH);

delay(10);

}

}

uint8_t readCapacitivePin(int pinToMeasure) {

// Variables used to translate from Arduino to AVR pin naming

volatile uint8_t* port;

volatile uint8_t* ddr;

volatile uint8_t* pin;

// Here we translate the input pin number from

// Arduino pin number to the AVR PORT, PIN, DDR,

// and which bit of those registers we care about.

byte bitmask;

port = portOutputRegister(digitalPinToPort(pinToMeasure));

ddr = portModeRegister(digitalPinToPort(pinToMeasure));

bitmask = digitalPinToBitMask(pinToMeasure);

pin = portInputRegister(digitalPinToPort(pinToMeasure));

// Discharge the pin first by setting it low and output

*port &= ~(bitmask);

*ddr |= bitmask;

delay(1);

// Make the pin an input with the internal pull-up on

*ddr &= ~(bitmask);

*port |= bitmask;

// Now see how long the pin to get pulled up. This manual unrolling of the loop

// decreases the number of hardware cycles between each read of the pin,

// thus increasing sensitivity.

uint8_t cycles = 17;

if (*pin & bitmask) { cycles = 0;}

else if (*pin & bitmask) { cycles = 1;}

else if (*pin & bitmask) { cycles = 2;}

else if (*pin & bitmask) { cycles = 3;}

else if (*pin & bitmask) { cycles = 4;}

else if (*pin & bitmask) { cycles = 5;}

else if (*pin & bitmask) { cycles = 6;}

else if (*pin & bitmask) { cycles = 7;}

else if (*pin & bitmask) { cycles = 8;}

else if (*pin & bitmask) { cycles = 9;}

else if (*pin & bitmask) { cycles = 10;}

else if (*pin & bitmask) { cycles = 11;}

else if (*pin & bitmask) { cycles = 12;}

else if (*pin & bitmask) { cycles = 13;}

else if (*pin & bitmask) { cycles = 14;

else if (*pin & bitmask) { cycles = 15;

else if (*pin & bitmask) { cycles = 16;

// Discharge the pin again by setting it low and output

// It’s important to leave the pins low if you want to

// be able to touch more than 1 sensor at a time – if

// the sensor is left pulled high, when you touch

// two sensors, your body will transfer the charge between

// sensors.

*port &= ~(bitmask);

*ddr |= bitmask;

return cycles;

}

Touch Electronic Piano

Connect the positive terminal of the passive buzzer to the A3 port of the Arduino UNO, and the negative terminal of the buzzer to ground; then connect the D2-D9 ports as touch keys. Burn the program below to achieve the function of the touch electronic piano.

Reference code:

int ledPin = A3;// Connect to the positive terminal of the passive buzzer

int capval1,capval2,capval3,capval4,capval5,capval6,capval7,capval8;

void setup()

{

pinMode(ledPin, OUTPUT);

Serial.begin(9600);

Serial.println(“Touch sensor”);

}

void loop ()

{

digitalWrite(ledPin,LOW);

capval1 = readCapacitivePin(2); //Pin D2

capval2 = readCapacitivePin(3);

capval3 = readCapacitivePin(4);

capval4 = readCapacitivePin(5);

capval5 = readCapacitivePin(6);

capval6 = readCapacitivePin(7);

capval7 = readCapacitivePin(8);

capval8 = readCapacitivePin(9); //Pin D9

if (capval1 > 2) tone(ledPin, 262, 10);

if (capval2 > 2) tone(ledPin, 294, 10);

if (capval3 > 2) tone(ledPin, 330, 10);

if (capval4 > 2) tone(ledPin, 350, 10);

if (capval5 > 2) tone(ledPin, 393, 10);

if (capval6 > 2) tone(ledPin, 441, 10);

if (capval7 > 2) tone(ledPin, 495, 10);

if (capval8 > 2) tone(ledPin, 525, 10);

}

uint8_t readCapacitivePin(int pinToMeasure) {

// Variables used to translate from Arduino to AVR pin naming

volatile uint8_t* port;

volatile uint8_t* ddr;

volatile uint8_t* pin;

// Here we translate the input pin number from

// Arduino pin number to the AVR PORT, PIN, DDR,

// and which bit of those registers we care about.

byte bitmask;

port = portOutputRegister(digitalPinToPort(pinToMeasure));

ddr = portModeRegister(digitalPinToPort(pinToMeasure));

bitmask = digitalPinToBitMask(pinToMeasure);

pin = portInputRegister(digitalPinToPort(pinToMeasure));

// Discharge the pin first by setting it low and output

*port &= ~(bitmask);

*ddr |= bitmask;

delay(1);

// Make the pin an input with the internal pull-up on

*ddr &= ~(bitmask);

*port |= bitmask;

// Now see how long the pin to get pulled up. This manual unrolling of the loop

// decreases the number of hardware cycles between each read of the pin,

// thus increasing sensitivity.

uint8_t cycles = 17;

if (*pin & bitmask) { cycles = 0;}

else if (*pin & bitmask) { cycles = 1;}

else if (*pin & bitmask) { cycles = 2;}

else if (*pin & bitmask) { cycles = 3;

else if (*pin & bitmask) { cycles = 4;

else if (*pin & bitmask) { cycles = 5;

else if (*pin & bitmask) { cycles = 6;

else if (*pin & bitmask) { cycles = 7;

else if (*pin & bitmask) { cycles = 8;

else if (*pin & bitmask) { cycles = 9;

else if (*pin & bitmask) { cycles = 10;

else if (*pin & bitmask) { cycles = 11;

else if (*pin & bitmask) { cycles = 12;

else if (*pin & bitmask) { cycles = 13;

else if (*pin & bitmask) { cycles = 14;

else if (*pin & bitmask) { cycles = 15;

else if (*pin & bitmask) { cycles = 16;

// Discharge the pin again by setting it low and output

// It’s important to leave the pins low if you want to

// be able to touch more than 1 sensor at a time – if

// the sensor is left pulled high, when you touch

// two sensors, your body will transfer the charge between

// sensors.

*port &= ~(bitmask);

*ddr |= bitmask;

return cycles;

}

Touch Electronic Piano

Connect the positive terminal of the passive buzzer to the A3 port of the Arduino UNO, and the negative terminal of the buzzer to ground; then connect the D2-D9 ports as touch keys. Burn the program below to achieve the function of the touch electronic piano.

Reference code:

int ledPin = A3;// Connect to the positive terminal of the passive buzzer

int capval1,capval2,capval3,capval4,capval5,capval6,capval7,capval8;

void setup()

{

pinMode(ledPin, OUTPUT);

Serial.begin(9600);

Serial.println(“Touch sensor”);

}

void loop ()

{

digitalWrite(ledPin,LOW);

capval1 = readCapacitivePin(2); //Pin D2

capval2 = readCapacitivePin(3);

capval3 = readCapacitivePin(4);

capval4 = readCapacitivePin(5);

capval5 = readCapacitivePin(6);

capval6 = readCapacitivePin(7);

capval7 = readCapacitivePin(8);

capval8 = readCapacitivePin(9); //Pin D9

if (capval1 > 2) tone(ledPin, 262, 10);

if (capval2 > 2) tone(ledPin, 294, 10);

if (capval3 > 2) tone(ledPin, 330, 10);

if (capval4 > 2) tone(ledPin, 350, 10);

if (capval5 > 2) tone(ledPin, 393, 10);

if (capval6 > 2) tone(ledPin, 441, 10);

if (capval7 > 2) tone(ledPin, 495, 10);

if (capval8 > 2) tone(ledPin, 525, 10);

}

uint8_t readCapacitivePin(int pinToMeasure) {

// Variables used to translate from Arduino to AVR pin naming

volatile uint8_t* port;

volatile uint8_t* ddr;

volatile uint8_t* pin;

// Here we translate the input pin number from

// Arduino pin number to the AVR PORT, PIN, DDR,

// and which bit of those registers we care about.

byte bitmask;

port = portOutputRegister(digitalPinToPort(pinToMeasure));

ddr = portModeRegister(digitalPinToPort(pinToMeasure));

bitmask = digitalPinToBitMask(pinToMeasure);

pin = portInputRegister(digitalPinToPort(pinToMeasure));

// Discharge the pin first by setting it low and output

*port &= ~(bitmask);

*ddr |= bitmask;

delay(1);

// Make the pin an input with the internal pull-up on

*ddr &= ~(bitmask);

*port |= bitmask;

// Now see how long the pin to get pulled up. This manual unrolling of the loop

// decreases the number of hardware cycles between each read of the pin,

// thus increasing sensitivity.

uint8_t cycles = 17;

if (*pin & bitmask) { cycles = 0;}

else if (*pin & bitmask) { cycles = 1;}

else if (*pin & bitmask) { cycles = 2;}

else if (*pin & bitmask) { cycles = 3;

else if (*pin & bitmask) { cycles = 4;

else if (*pin & bitmask) { cycles = 5;

else if (*pin & bitmask) { cycles = 6;

else if (*pin & bitmask) { cycles = 7;

else if (*pin & bitmask) { cycles = 8;

else if (*pin & bitmask) { cycles = 9;

else if (*pin & bitmask) { cycles = 10;

else if (*pin & bitmask) { cycles = 11;

else if (*pin & bitmask) { cycles = 12;

else if (*pin & bitmask) { cycles = 13;

else if (*pin & bitmask) { cycles = 14;

else if (*pin & bitmask) { cycles = 15;

else if (*pin & bitmask) { cycles = 16;

// Discharge the pin again by setting it low and output

// It’s important to leave the pins low if you want to

// be able to touch more than 1 sensor at a time – if

// the sensor is left pulled high, when you touch

// two sensors, your body will transfer the charge between

// sensors.

*port &= ~(bitmask);

*ddr |= bitmask;

return cycles;

}

Breathing Light

Using pulse-width modulation (PWM) can control the brightness of the light-emitting diode to gradually brighten and dim, achieving breathing effects. The circuit connection is simple; after downloading the program to the board, you can see the LED light gradually brighten from dark, then dim to off, cycling in this manner, as if it is breathing.

Arduino Basic Experiment Tutorial

Reference code

int brightness = 0; //Represents the brightness of the LED

int fadeAmount = 5; //Increment of LED brightness change

void setup()

{

pinMode(11, OUTPUT); // Set pin 11 as output port

}

void loop()

{

analogWrite(11, brightness); //Write the brightness value to the port

brightness = brightness + fadeAmount; //Change brightness in the next loop

if (brightness <= 0 || brightness >= 255)

fadeAmount = -fadeAmount ; //Reverse at maximum or minimum brightness

delay(30); //Delay 30 milliseconds

Light-Controlled Electronic Piano

Using a light-sensitive resistor to detect light intensity, and controlling the passive buzzer to produce different tones based on this, creates a light-controlled electronic piano.

Principle Diagram

Arduino Basic Experiment Tutorial

Physical Diagram

For convenience, use Arduino UNO R3 instead of mCookie.

Arduino Basic Experiment Tutorial

Reference Program

//Define musical scale constants

#define Do 262

#define Re 294

#define Mi 330

#define Fa 349

#define Sol 392

#define La 440

#define Si 494

int val=0;

int buzzerPin=7; //Define buzzer pin

void setup(){

Serial.begin(115200);

pinMode(buzzerPin,OUTPUT);

}

void loop()

{

int n = analogRead(A0); //Read analog port A0 to get light intensity

Serial.println(n); // For IDE serial monitor

val=0;

if(n<=200)

val=Do;

else if(n<=220)

val=Re;

else if(n<=240)

val=Mi;

else if (n<=300)

val=Fa;

else if (n <=350)

val=Sol;

else if (n<=400)

val=La;

else if (n<=500)

val=Si;

if (val)

{

tone(buzzerPin,val,1000);

delay(1000);

}

else

delay(100); //Prevent writing to the serial port too quickly

}

Advanced Experiments

Joule Thief Circuit

  • The Amazing Joule Thief Circuit

  • Principle of Joule Thief Circuit

  • Hello everyone, let me introduce this…

Single Transistor Common Emitter Amplifier Circuit

  • 5-26V DC Boost Module Powered via USB

  • XL6009 Boost/Buck Circuit

  • Inductor DC Boost Circuit

LM386 Audio Amplifier Circuit

  • Audio Amplifier Circuit Based on LM386

Colorful Flashing Stick

  • Inheriting the Stick Culture

  • Shahhe Campus, Senior Student Takes You Flying!

  • Senior Student Teaches You How to Make Colorful Flashing Sticks

  • Developer Mode and User Mode of Colorful Flashing Sticks

  • Soldering of Colorful Flashing Sticks

  • How to Modify the Text on Colorful Flashing Sticks

  • Usage of Adafruit_NeoPixel Library

  • Bluetooth Version of Colorful Flashing Stick is Born!

Laser PM2.5 Detector

  • Step-by-Step Guide to Making a PM2.5 Detector

Pocket Experimental Instrument AD2

  • Installation Tutorial for Pocket Experimental Instrument AD2

  • Pocket Experimental Instrument AD2 for Circuit Courses

  • Requirements for the First Experiment in Analog Electronics

Using Arduino UNO Board

  • First Time Using Arduino UNO R3

  • Arduino Example — Ultrasonic Distance Measurement

  • Arduino Example — Pyroelectric Sensor

  • Arduino Example — Detecting Ambient Temperature and Humidity with DHT11

  • Arduino Example — Lighting Up OLED Screen

  • The Smallest Arduino Development Board Digispark

Arduino Basic Experiment Tutorial

Leave a Comment

Your email address will not be published. Required fields are marked *