The following experiment is designed to help students initially grasp how to use a microcontroller and achieve some interesting functions with basic electronic components.
The microcontroller control module chosen is Arduino / Microduino / mCookie. These three products are compatible with each other and are essentially the same to use, all supported by the powerful integrated development environment (IDE) of Arduino. Arduino’s IDE encapsulates many complex low-level operations into functions for beginners to call, greatly lowering the learning threshold and simplifying the programming workload, making it very suitable for zero-based entry-level learning.
By purchasing only an Arduino UNO R3 core board (third-party compatible boards cost about 20 yuan), you can realize various complex functional circuits through a breadboard and necessary components.
If using mCookie, you need to prepare the mCookie Core+ core board (AVR microcontroller ATmega644PA, etc.), and the mCookie USBTTLC serial communication module (program download module); stacking the two together can meet our requirements. However, since the mCookie modules use magnetic adsorption, you will also need to use the mCookie adapter board to bring the necessary pins out to the breadboard, which costs about 200 yuan.
mCookie USB module and Core+ core module
mCookie pin adapter board
If using Microduino, you also need to prepare the Microduino Core+ core board (AVR microcontroller ATmega328P, etc.), and Microduino USBTTLC serial communication module. Since Microduino itself is in a pin format, no adapter board is needed, costing about 105 yuan.
If using Arduino, you only need to purchase an Arduino UNO R3 core board, costing about 88 yuan, with third-party compatible boards costing about 20 yuan, and no adapter board is needed.
Official Arduino UNO R3 board
Arduino UNO R3 third-party compatible board
Related resource links:
mCookie products https://wiki.microduino.cn/
Arduino Chinese community http://www.arduino.cn/
Arduino official website https://www.arduino.cc/
Arduino China http://www.arduino.org.cn/
Open source software hosting platform https://github.com/
Additionally, everyone must get used to solving problems on their own and use search engines to find answers to questions.
Many things are like traveling; when you decide to set off, the hardest part is already done.
Freshman Experiment Assignment
1. Automatic Control of Street Lights
Requirements: When the sunlight is strong during the day, the street lights are off; when the sunlight is weak at night, the street lights turn on.
Design Idea: Use a light-sensitive element to perceive light intensity and control the street lights through mCookie.
Automatic control of street lights circuit diagram and wiring diagram
Note: The street light is replaced by an LED, and the LED must be connected in series with a current-limiting resistor. The mCookie/Microduino Upin27 product series pin definitions are consistent.
Reference code:
int a =600; //Brightness value
void setup ()
{
Serial.begin(115200); //Set serial port baud rate
pinMode(7,OUTPUT); //Set output port
}
void loop()
{
int n = analogRead(A7); //Read analog port A0 to get light intensity
Serial.println(n); // For IDE serial monitor
if (n<= a ) // Judge light intensity
digitalWrite(7,HIGH);
else
digitalWrite(7,LOW);
delay(100); //Prevent serial write speed from being too fast
}
Note: The ports in the program may need to be modified according to the actual circuit connections and must correspond to the ports connected to the circuit.
Thinking Questions:
(1) How to change it to a light intensity warning circuit, lighting up the LED when the light is strong?
(2) How to make the LED turn into a breathing light? (Hint: Use PWM to control the LED)
(3) How to design an automatic dimming table lamp? (Hint: Introduce a light-sensitive resistor)
(4) How to design a colorful table lamp? That is, control the colored LEDs to display different colors of light based on 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 temperature, the brighter the LED, presenting it to the user in an appropriate way.
Digital thermometer circuit diagram and wiring diagram
Note: (1) The temperature sensor uses LM35D, which resembles a transistor, outputting an analog voltage value. Each increase of 10mV in voltage represents a temperature rise of 1℃, and the two have a linear relationship.
(2) This circuit uses the internal reference source for ADC sampling. To achieve accurate temperature measurement, the reference voltage for analog-to-digital conversion must be stable; the REF pin of mCookie should be connected to a precise voltage source.
(3) The physical diagram does not connect the LED and buzzer.
Reference code:
// Temperature control alarm experiment, D6 connects to an active buzzer, D7 connects to an LED
unsigned int tempMin = 26; //Light up temperature, can be set to the room temperature observed on the serial port
unsigned int tempMax = 30;//Alarm temperature
void setup() {
Serial.begin(9600); //Serial port initialization
analogReference(INTERNAL1V1);//Internal reference voltage source
pinMode(6,OUTPUT);
digitalWrite(6,LOW);
}
void loop() {
double analogVotage = 1.1*(double)analogRead(A7)/1023.0;//Read the analog voltage
double temp = 100*analogVotage; //Calculate temperature
unsigned int dutyCycle; //Duty cycle
if (temp <= tempMin) {
dutyCycle = 0;
digitalWrite(6,LOW);
}
else if (temp < tempMax){
dutyCycle = (temp-tempMin)*255/(tempMax-tempMin);
digitalWrite(6,LOW);
}
else{
dutyCycle = 255;
digitalWrite(6,HIGH);
}
analogWrite(7,dutyCycle);//Generate PWM
Serial.print(“Temp: “);//Serial output
Serial.print(temp);
Serial.print(” Degrees Duty cycle: “);
Serial.print(dutyCycle);
Serial.print(“/255\n”);
delay(100);//Wait 0.1 seconds, control refresh speed
}
Thinking Questions:
(1) How to roughly display changes in ambient temperature through colored LEDs?
(2) How to use a seven-segment display to show ambient temperature?
(3) How to use an OLED screen to display ambient temperature?
3. Intelligent Temperature Control Fan
Requirements: Automatically adjust the fan speed based on ambient temperature.
Intelligent temperature control fan circuit diagram and wiring diagram
Note: Motors generally require a large current, and the microcontroller’s IO port driving capacity is limited, so it needs to be amplified by a transistor. Since PWM is used, the transistor operates in a switch state.
Reference code:
/* Temperature-controlled fan program, A7 reads voltage value, D7 outputs PWM pulse
Note: The internal reference voltage source is not very accurate and can be affected by power supply voltage; the temperature value may have deviations.
When the motor rotates too fast, the current may be large enough to pull down the power supply voltage.
*/
double analogVotage; //Analog voltage value
double temp; //Temperature
unsigned int dutyCycle; //Duty cycle
// The following two temperature thresholds change with the experimental environment
unsigned int tempMin = 28; //Zero speed temperature, set to the room temperature observed on the serial port
unsigned int tempMax = 35; //Full speed temperature, set to the temperature of the component held by hand
void setup() {
Serial.begin(9600); //9600 baud rate for serial communication
analogReference(INTERNAL1V1); //Call 1.1V reference voltage source
}
void loop() {
analogVotage = 1.1*(float)analogRead(A7)/1023.0; //Convert ADC read value 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(7,dutyCycle); //Generate PWM
Serial.print(“Temp: “); //Serial output in format
Serial.print(temp);
Serial.print(” Degrees Duty cycle: “);
Serial.print(dutyCycle);
Serial.print(“/255\n”);
delay(100); //Wait 0.1 seconds, control refresh speed
}
Thinking Questions:
(1) Why not use the microcontroller’s IO port to drive the motor directly?
(2) How to make the fan start only when someone is detected and automatically adjust speed based on ambient temperature?
(3) Add an OLED screen to display ambient temperature and fan speed in real-time.
Supplement: The pyroelectric infrared sensor has a Fresnel lens in front, which can sensitively detect human activity. There are many such sensors available on Taobao, and everyone can purchase according to their needs.
4. Inverting Proportional Operation Circuit
Requirements: Use an integrated operational amplifier to achieve linear amplification of the input signal.
Inverting proportional operation circuit diagram and wiring diagram
Note: The input can use a resistor or potentiometer divider; 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 go into saturation. The voltage values of the input and output are collected by mCookie and viewed through the IDE’s serial monitor.
Reference code:
///A0 connect amp output
///A1 connect single battery output
int anaValueSingleBa; //single battery value map(0–1023)
int anaValueAmp; //amp value map(0–1023)
float anaVoltageSingleBa; //single battery voltage
float anaVoltageAmp; //amp output voltage
void setup()
{
Serial.begin(9600);
}
void loop()
{
anaValueSingleBa=analogRead(A1);
anaVoltageSingleBa=anaValueSingleBa/1023.0*5;
Serial.print(“Single battery voltage is “);
Serial.print(anaVoltageSingleBa);
Serial.println(“V”);
delay(1000);
anaValueAmp=analogRead(A0);
anaVoltageAmp=anaValueAmp/1023.0*5;
Serial.print(“Amp output voltage is “);
Serial.print(anaVoltageAmp);
Serial.println(“V”);
delay(1000);
}
Thinking 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 finger.
Design Idea: Use Arduino’s readCapacitivePin() function to read the capacitance value of the port; use the tone(pin, frequency) function to produce different tones.
Touch electronic piano experiment circuit and wiring diagram
Note: To prevent static electricity from damaging the mCookie core module, it is recommended to touch a radiator or similar to release static electricity first, or cover the wire with an insulating board.
Reference code:
int ledPin = A0;
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(3);
capval2 = readCapacitivePin(4);
capval3 = readCapacitivePin(5);
capval4 = readCapacitivePin(6);
capval5 = readCapacitivePin(7);
capval6 = readCapacitivePin(8);
capval7 = readCapacitivePin(9);
capval8 = readCapacitivePin(10);
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;
}
Extended Experiment:
Full-color LED strip
This LED uses a daisy chain method to connect together. There are many independent LED beads sold on Taobao, as well as ready-made LED strips. Interested students can search for WS2812b on Taobao. The control program is included in the Arduino integrated development environment’s example program: File->Examples->_07_Sensor_LED_WS2812
Colorful shaking stick
With slight modifications to the above full-color LED strip, using a mercury switch to determine the direction of shaking, and utilizing the powerful function library of the Arduino integrated development environment, it is very easy and quick to complete the design of a colorful shaking stick. Of course, you can also add a Bluetooth module and develop an app on your phone to modify the display pattern of the shaking stick via Bluetooth connection.
Digital thermometer
Use LM35D to collect ambient temperature and display it on an OLED screen.
Fully utilize your imagination and creativity; you can design many interesting and practical small projects. For any components you need, you can purchase them from Taobao or go to the Zhongfa Electronics Market.
University is different from high school; university is an ocean of knowledge. No one will supervise your learning anymore; you must take the initiative to learn. Additionally, university is not what high school teachers say, “When you get to university, it will be easy; you can play freely.” University is the beginning of life; from this moment on, you should truly get busy. This is the state that university should have.
To deeply understand the significance of the experiment and master a technology, it is recommended to self-study C language and master this programming language within 1-2 weeks. The top four programming languages in the global ranking are: Java, C, C++, Python.
Programming is relatively more like a liberal arts subject in engineering and science, similar to writing.