12 Arduino Projects for Beginners — With Code

About: We love technology, but first we are Makers.

Have you ever wanted to learn how to write code for the Arduino board by working on fun Arduino projects, but never really knew where or how to start?

Well, I believe that every Maker has already been there. That’s the reason why I have create a collection of Arduino projects with increasing level of difficulty for anyone to start getting more familiar with programming the Arduino board. The projects will allow you to learn the basics of the Arduino in a very efficient manner, and take you step by step to building more complex projects.

Step 1: Arduino Projects

This collection of Arduino projects from very basic to more advanced includes:

  • LED Blink
  • LED button
  • LED brightness
  • Traffic Light Controller
  • Servomotor Control
  • Smart Night Lamp
  • Play Tunes
  • Voice control
  • and much more…

These Arduino projects can inspire you for your own projects. If you want to learn more, you can take our complete Udemy online course: Arduino and Robotics.

The projects source code can be found on Github. Without further due, let’s start!

Step 2: LED Blink

The “LED Blink” is for the Arduino the equivalent of the “Hello world” in computer programming, where you will need to write a code to have the sentence “Hello world” displayed in the console of a computer. Not really exciting, but it gives a hint on the inner workings of the Arduino board. Each Arduino board comes with an on-board LED that can be used for this purpose. The project toggles the default LED of the Arduino board located on the pin 12 (ledPin) ON and then OFF every 2 seconds (delayTime, note: 2s=2000ms).

Arduino LED blink code

Step 3: LED Button Control

Once more comfortable with the “LED Blink” project, the more natural way to move forward is to add more control on our LED. We can use a push button for this purpose and only switch the state of the LED when the button is pressed down. The “if” statement in line 14 is used to check for a change of state of the button, in this case state transition “button up” to “button down”. The code only toggles the state of the button 200 ms after the button is pressed in order to filter the input. This process is called denouncing.

arduino_led_button_toggle.ino

int inputPin = 0;
int ledPin = 12;
int buttonState = 0;
int buttonStateTm1 = 0;
int ledState = LOW;
voidsetup() {
pinMode(inputPin, INPUT);
pinMode(ledPin, OUTPUT);
}
voidloop() {
buttonState = digitalRead(inputPin);
if(buttonState > buttonStateTm1){
delay(200);
ledState = !ledState;
}
digitalWrite(ledPin, ledState);
buttonStateTm1 = buttonState;
}

Step 4: LED Brightness Control

The Arduino projects are starting to get more interesting as we move forward in this post. So far, we have only controlled the LED in a binary form. ON or OFF depending on various events and inputs. But wouldn’t it be nice to control the brightness of our LED? Well that’s exactly what we will do in this project!

Controlling the brightness of an LED is equivalent to controlling the voltage on the pins of the LED. To do that, we will use the PWM output of the Arduino board. With the LED connected to pin 3 and a potentiometer connected to the analog input A0, the code bellow changes the brightness of our LED by rotating the potentiometer.

led_brightness_control.ino

int ledPin = 3;
int valPot = 0;
voidsetup() {
}
voidloop() {
valPot = analogRead(A0);
valPot = map(valPot, 0, 1023, 0, 255);
analogWrite(ledPin, valPot);
}

Step 5: Traffic Light

To continue and end our Arduino projects on LEDs, let’s build a traffic light controller with 3 LEDs (Red, Yellow, Green). Each LED should light up one after the other after a specified amount of time to emulate a traffic light. For further improvements, a button can also be connected to the Arduino to emulate, a “Pedestrian crossing” button that makes the traffic light turn to Red on demand.

traffic_light_controller.ino

//Declare the pins on which the LEDs are connected
int ledPinRed = 2;
int ledPinYellow = 1;
int ledPinGreen = 0;
voidsetup() {
// Set the pins 0, 1, 2 as output
pinMode(ledPinRed, OUTPUT);
pinMode(ledPinYellow, OUTPUT);
pinMode(ledPinGreen, OUTPUT);
//Turn OFF all the LEDs
digitalWrite(ledPinRed, LOW);
digitalWrite(ledPinYellow, LOW);
digitalWrite(ledPinGreen, LOW);
}
voidloop() {
// Successively Turn on each LED
changeLedState(ledPinRed, 3000);
changeLedState(ledPinYellow, 1000);
changeLedState(ledPinGreen, 3000);
}
voidchangeLedState(int ledPin, int duration){
//Turn ON the selected LED and turn off
//all the other ones
if(ledPin == ledPinRed){
digitalWrite(ledPinRed, HIGH);
digitalWrite(ledPinYellow, LOW);
digitalWrite(ledPinGreen, LOW);
}
elseif(ledPin == ledPinYellow){
digitalWrite(ledPinRed, LOW);
digitalWrite(ledPinYellow, HIGH);
digitalWrite(ledPinGreen, LOW);
}
elseif(ledPin == ledPinGreen){
digitalWrite(ledPinRed, LOW);
digitalWrite(ledPinYellow, LOW);
digitalWrite(ledPinGreen, HIGH);
}
else{
digitalWrite(ledPinRed, LOW);
digitalWrite(ledPinYellow, LOW);
digitalWrite(ledPinGreen, LOW);
}
delay(duration);
}

Step 6: Servomotor Control

Hoora!!! It is great to have been able to explore small but interesting projects with LEDs. But wouldn’t it be great to see something physically moving and controlled by the Arduino?

We can use a servomotor for this. A servomotor is controlled almost in the same way as the LED is controlled in the “LED brightness control” project, but there is more to it than just setting the PWM output of the Arduino to a specific value. This is when libraries comes very handy, we can use the “Servo.h” library provided with the Arduino development environment to make it very simple to control a servomotor. We include the library in our project and use the functions “attach()” and “write()” to respectively configure the PWM output port to be used with a Servomotor and set the position of the servomotor. The position of the servomotor will oscillate between -180° to +180°.

servomotor.ino

#include<Servo.h>
int servoPin = 3;
Servo myservo;
voidsetup() {
myservo.attach(servoPin);
}
voidloop() {
for( int pos = 0 ; pos <= 180; pos++){
myservo.write(pos);
delay(15);
}
for( int pos = 180; pos>= 0; pos--){
myservo.write(pos);
delay(15);
}
}
view rawservomotor.ino hosted with ❤ by GitHub

Step 7: Smart Night Lamp

Let’s now put everything together into a single project. The Smart Night Lamp will use all the knowledge that we have acquired so far. We will change the brightness of 3 LEDs by using a potentiometer, and set the angle of a servomotor to it’s current value to indicate the brightness level. Note that a single multi-color LED can also be used instead of the 3 LEDs, this will result in effectively having different colors. A multi-color LED has 3 pins and allows to control it’s Red, Green and Blue value to create any color in the visible spectrum. For simplicity, we will stick here to what we have already learned so far, but for more advanced learners, I would recommend to give a go and use the multi-color LED instead.

smart_night_lamp.ino

#include<Servo.h>
int buttonState = 0;
int buttonState_tm1 = 0;
constint buttonPin = 0;
constint ledPinRed = 6;
constint ledPinBlue = 5;
constint ledPinGreen = 3;
constint servoMotorPin = 9;
int valLedRed = 0;
int valLedGreen = 0;
int valLedBlue = 0;
int pwmRed = 0;
int pwmGreen = 0;
int pwmBlue = 0;
int valPotentiometer = 0;
int cmdPosServo = 0;
int _max_pwm_val = 100;
Servo myservo;
voidsetup() {
pinMode(buttonPin, INPUT);
myservo.attach(servoMotorPin);
}
voidloop() {
buttonState = digitalRead(buttonPin);
valPotentiometer = analogRead(A0);
if(buttonState > buttonState_tm1){
delay(200);
valLedRed = random(_max_pwm_val);
valLedGreen = random(_max_pwm_val);
valLedBlue = random(_max_pwm_val);
}
cmdPosServo = map(valPotentiometer, 0, 1023, 0, 180);
valPotentiometer = map(valPotentiometer, 0, 1023, 0, _max_pwm_val);
pwmRed = (int) constrain(valLedRed + (valPotentiometer - _max_pwm_val/2.0), 0.0, _max_pwm_val);
pwmGreen = (int) constrain(valLedGreen + (valPotentiometer - _max_pwm_val/2.0), 0, _max_pwm_val);
pwmBlue = (int) constrain(valLedBlue + (valPotentiometer - _max_pwm_val/2.0), 0, _max_pwm_val);
changeColor(pwmRed, pwmGreen, pwmBlue);
myservo.write(cmdPosServo);
buttonState_tm1 = buttonState;
delay(15);
}
voidchangeColor(int red, int green, int blue){
analogWrite(ledPinRed, red);
analogWrite(ledPinGreen, green);
analogWrite(ledPinBlue, blue);
}

Step 8: Play Tunes

That’s an exciting Arduino project, being able to play various tunes with the Arduino board. Although this may seam daunting at first and seems to require a deep knowledge of the musical language, it is fairly easy to produce sound using the Arduino board. By connecting a buzzer on a PWM output of the Arduino board and using the built-in function “tone(pin, frequency, duration)” it is possible to generate a tone of any frequency for a specific duration. A melody is just a succession of those tones. In this example the frequency and the duration of each tone is stored in arrays and are later on played by reading each value in the arrays.

Play tunes using the Arduino board and a piezzo-electric buzzer

#defineNUM_NOTES8
int buzzerPin = 3;
int melody[NUM_NOTES] = {
262, 196, 196, 220, 196, 0, 247, 262
};
int noteDurations[NUM_NOTES] = {
4, 8, 8, 4, 4, 4, 4, 4
};
voidsetup() {
}
voidloop() {
for(int noteId = 0; noteId < NUM_NOTES; noteId++){
int noteDuration = 1000/ noteDurations[noteId];
tone(buzzerPin, melody[noteId], noteDuration);
int pauseBetweenNotes = noteDuration* 1.3;
delay(pauseBetweenNotes);
noTone(buzzerPin);
}
}
view rawplay_tunes.ino hosted with ❤ by GitHub

Step 9: Voice Control

Who has never dreamed of interacting with an advanced artificial intelligence such as Jarvis first seen in the movie Iron Man? Well it would be even better to build one by yourself :)!

Connecting a microphone to the Arduino board will allow us to just do that. Voice is just another analog signal that we can capture by using an analog input. By connecting a microphone on the analog pin A0, we can capture our voice as a signal that changes over time. The simplest way to integrating voice control in any application is to just use the amplitude of the vocal signal to generate an action once this amplitude is above a specific value. In this project, we will turn on an LED by speaking into a microphone. We detect the amplitude of the vocal signal (how loud the signal is) and turn on an LED if the signal is above a threshold. Any loud sound can change the state of the LED in this project, this can serve as the basis to implementing more complex behaviours.

voice_control.ino

int soundLevel = 0;
int ledPin = 12;
bool toggle = false;
voidtoggleLEDState(){
digitalWrite(ledPin, toggle);
toggle = !toggle;
}
voidsetup() {
pinMode(ledPin, OUTPUT);
}
voidloop(){
soundLevel = analogRead(A0);
if(soundLevel >= 650) {
delay(100);
toggleLEDState();
}
delay(1);
}

Step 10: Conclusion

Congratulations, if you have reached so far in this post, they are even more exciting projects in the github repository that involves using the Arduino 101 board and create cool project with functionalities only available on this board such as accelerometers, timers, Bluetooth, etc…

Below are some useful links:

Udemy course: Arduino and Design : Make Your First Robot

Github: Projects Code

Hope you enjoyed this post, it will be updated as more projects are added to the github repository, so stay tuned and keep coding awesome projects.

Share

    Recommendations

    • Woodworking Contest

      Woodworking Contest
    • Gardening Contest

      Gardening Contest
    • Classroom Science Contest

      Classroom Science Contest

    Discussions