Category:

Ardunio

Title:

Ardunio Startup Activities for Beginners

At first glance, it may be complicated to start robotic coding with Ardunio. Especially after looking at the samples and from there the cables and scattered image of this work is very difficult to create the perception. However, when we go down to the bottom of the work, we examine each piece one by one and when we divide the work into simple pieces, we actually see that it is not too difficult to do this. This article is intended to be a quick guide to remember the frequently forgotten links and simple codes, although it is aimed at people who will start coding for Ardunio and project development for the first time.

It is important to note that before you start the activities, you do not have to have an Ardunio set to do a project with Ardunio. Offline programs such as Fritzing, virtual environments such as Tinkercad, where you can create Ardunio circuits virtually. Especially with Tinkercad you can simulate the circuits you have prepared with the system. 

For those who will start for the first time, we recommend that they do this in order. It is useful to create your circuit primarily in environments such as Fritzing or Tinkercad. You can leave comments, comments and suggestions in the comments section. 

1) Turning on led without code with Ardunio:

Objective: To light a led without writing code with Ardunio.

Materials:

  • Ardunio Uno
  • Led
  • 220 Ohm Resistor
  • Breadboard
  • Jumper cables

Create the circuit structure by looking at the above circuit diagram created with Fritzing. Then lets make the connection with Ardunio by connecting to the computer. As soon as Ardunio works, we have to see our LEDs on. In fact, Ardunio was used only as a source of electricity in this event. But with this event, we have an idea about what the GND and 5V pins on ledin and Ardunio do. We use resistance to prevent the LED from burning. If we do not use resistance, our led will probably become unusable. Instead of a 220 Ohm resistor, we can use it at a resistance level of 330 Ohm or higher. The higher the resistance, the lower the brightness of the LED.

2) Turning on leds in order with ardunio

Objective: Turning on 3 leds at 0,5 seconds interval.

Materials:

  • Ardunio Uno
  • 3 x Led
  • 3 x 220 Ohm Resistor
  • Breadboard
  • Jumper cables

Create the circuit according to the above scheme. Then install the following code to Ardunio.

void setup() {
  // We must set output pins 
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(6, OUTPUT);
}
void loop() {
  digitalWrite(2, HIGH);  //Turn on electicity to pin 2
  delay(500);             //Delaying 0.5 seconds                   
  digitalWrite(2, LOW);   //Turning off electicity at pin 2
  digitalWrite(4, HIGH);  
  delay(500);                      
  digitalWrite(4, LOW);
  digitalWrite(6, HIGH);  
  delay(500);                      
  digitalWrite(6, LOW);
}

In there with "delay(500)" code keeps led open for 0,5 seconds. If we write 1000 instead of 500, led  will be open for 1 seconds.

3) Turning on a rgb led in three different colors with

Objective: Illumunate a rgb led in red, green, blue colors at 0.5 seconds interval

Materials:

  • Ardunio Uno
  • RGB led
  • 3 x 220 Ohm Ressistor
  • Breadboard
  • Jumper cables

The RGB led has 4 legs, unlike the normal 2-legged LED. One of these legs (2nd from the right) is connected to the ground. The other leds are used in order to obtain red, green and blue color values ​​in the order shown by the cable colors to which they are connected. We will send these pins with digital pwm pins according to the values ​​between 0-255 values.

Create the circuit according to the above scheme. Then install the following code to Ardunio.

int red=2;
int blue=4;
int green=6;
 
void setup() {
pinMode(red,OUTPUT);
pinMode(green,OUTPUT);
pinMode(blue,OUTPUT);
}
 
void loop() {
analogWrite(red,255);  //We are giving 255 value to this pin for led only lights red
analogWrite(green,0);
analogWrite(blue,0);
delay(500);
analogWrite(red,0);
analogWrite(green,0);
analogWrite(blue,255);  //We are giving 255 value to this pin for led only lights blue
delay(500); analogWrite(red,0); analogWrite(green,255); //We are giving 255 value to this pin for led only lights green
analogWrite(blue,0); delay(500); }

4) Switch on and off a led with button

Objective:  Switching on and off with a normal led button. It will open when we press the button, it will close when we press again.

Materials:

  • Ardunio Uno
  • Led
  • 220 Ohm Resistor (for led)
  • Buton
  • 10K Ohm Resistor (for button)
  • Breadboard
  • Jumper cables

In most of the sites, when the button is given the led turning on examples, the thing that is omitted is that when we press the button once, the LED is lit, once again it will go out. Usually when most of the sites are held down by pressing the button, the example goes out when the button is not pressed and the situation is slightly different. In order to achieve the desired situation, we use two variables with the name of "buttonSt" and "ledSt".

Create the circuit according to the above scheme. Then install the following code to Ardunio.

int ledPin=2;
int butonPin=4;
int butonSt=0;
bool ledSt=0;
void setup(){
    pinMode(ledPin,OUTPUT); // Setting led output
    pinMode(butonPin,INPUT); // Setting button input
 
}
void loop(){
    
    butonSt=digitalRead(butonPin); // Reading value from button
    if(butonSt==HIGH){
       if(ledSt==0)
       {  
       ledSt=1;
       }
       else
       {
       ledSt=0; 
       }
       delay(250); // Putting a little delay to prevent rapid transitions during press button 
    };

      if(ledSt==1)
    digitalWrite(ledPin,HIGH);
    else
    digitalWrite(ledPin,LOW);
}

5) Give a led gradual brightness with potentiometer

Objective:  Providing gradual brightness to a led with a potentiometer

Materials: 

  • Ardunio Uno
  • Led
  • 220 Ohm Resistor (for led)
  • 10K Potantiometer
  • Breadboard
  • Jumper cables

Potentiometer was used for the first time in this activity. The working principle of the potentiometer will be understood by this activity.In addition, the proportionality of the map function has been established..Create the circuit according to the above scheme. Then install the following code to Ardunio.

int analogValue = 0; 
int digitalValue = 0; 
void setup() {
pinMode(3, OUTPUT); 
}
void loop(){
analogValue = analogRead(A0);
digitalValue= map(analogValue,0,1023,0,255); //Comparing analog value from A0 to digital value
analogWrite(3, digitalValue); // Giving compared value to digital pin 3
}

6)Three stage led grading according to the potentiometer value

Objective:  It will be ensured that three LEDs will be burned gradually according to the value from the potentiometer. At the highest value, 3 LEDs will be lit and no LEDs will be lit at the lowest value. The leds will light according to the value of the intermediate values.

Materials: 

  • Ardunio Uno
  • 3xLed
  • 3x220 Ohm Resistor (for leds)
  • 10K Potantiometer
  • Breadboard
  • Jumper cables


int analog_value = 0; 
int digital_value = 0; 
void setup() {
pinMode(2, OUTPUT); //Defining output pins
pinMode(4, OUTPUT);  
pinMode(6, OUTPUT); 
}
 
void loop(){
analog_value = analogRead(A0); //Giving potentiometer value to variable
digital_value= map(analog_value,0,1023,0,255); //comparing analog value to digital
if(digital_value<20){   //If digital value is under 20, no led will be open
   digitalWrite(2, LOW);
   digitalWrite(4, LOW);
   digitalWrite(6, LOW);
  
} 
if(digital_value>19 && digital_value<85) { // One led open
   digitalWrite(2, HIGH);
   digitalWrite(4, LOW);
   digitalWrite(6, LOW);
  
}
if(digital_value>84 && digital_value<170) { //Two led open
   digitalWrite(2, HIGH);
   digitalWrite(4, HIGH);
   digitalWrite(6, LOW);
  
}
if(digital_value>169){    //Three led open
   digitalWrite(2, HIGH);
   digitalWrite(4, HIGH);
   digitalWrite(6, HIGH);
  
}
} 

7) Turning on a RGB led in three colors gradually accoording to potentiometer value

Objective:  The RGB LED will be illuminated red, blue and green depending on the value from the potentiometer.

Materials: 

  • Ardunio Uno
  • RGB Led
  • 220 Ohm Resistor 
  • 10K Potantiometer
  • Breadboard
  • Jumper cables


To make the RGB LED shine in 3 different colors proportionality was made with the map () function as in the previous activity. The value in the range 0-1024 from the potentiometer was divided into three, and the first section was used for red, second for blue and third for green color values. Independent "if" structures were used for each color.

Create the circuit according to the above scheme. Then load the following code into Ardunio.

int red=3;
int blue=5;
int green=6;
int analogValue = 0; 
int digitalValue = 0;
int redValue = 0;
int blueValue = 0;
int greenValue = 0;
void setup() {
pinMode(3, OUTPUT); 
pinMode(5, OUTPUT); 
pinMode(6, OUTPUT); 
}
void loop(){
Serial.begin(9600);
analogValue = analogRead(A0);
digitalValue= map(analogValue,0,1023,0,255); //Rate the value 0-1024 from A0 to 0-255 with map function
if(digitalValue<82)
{
  redValue= map(digitalValue,0,81,0,255); //Rate the range of digital values ​​for red color tones
  analogWrite(red,redValue);  //Only red tones
  analogWrite(green,0);
  analogWrite(blue,0);
}
if(digitalValue>81 && digitalValue<164)
{
  blueValue= map(digitalValue,82,163,0,255); //Rate the range of digital values ​​for blue color tones
  analogWrite(red,0);  //Only blue tones
  analogWrite(green,0);
  analogWrite(blue,blueValue);
}
if(digitalValue>163)
{
  greenValue= map(digitalValue,164,255,0,255);  //Rate the range of digital values ​​for green color tones
  analogWrite(red,0);  //Only green tones
  analogWrite(green,greenValue);
  analogWrite(blue,0);
}
}

8) Getting straight and frequency sounds from buzzer

Objective:  Getting straight and frequency sounds from the buzzer module with Ardunio

Materials: 

  • Ardunio Uno
  • Buzzer
  • 100 Ohm or less Resistor 
  • Breadboard
  • Jumper cables

Buzzer module is a module that emits different tones according to the incoming electrical frequencies. Alarming, warning sounds can be made with buzzer. Even melodies on various notes can be played with buzzer. You can click reach an piano example. Here we will make examples that will enable us to simply understand the logic of the buzzer's operation. First, let's create our circuit as above. The reason for the use of resistance in this circuit is to prevent the noise from the buzzer. If there is not enough sound, you can use a lower resistance. You can also connect the buzz without using resistance. But then it can be a little disturbing. The sound may be low if you use a large resistor.

Source code that provides continuous sound from the buzzer:

int buzzerPin = 2;
void setup()
{
   pinMode(buzzerPin, OUTPUT);
}
void loop()
{
   digitalWrite(buzzerPin,HIGH);
}

Source code that provides a 1 second delayed sound from the buzzer:

int buzzerPin = 2;
void setup()
{
   pinMode(buzzerPin, OUTPUT);
}
void loop()
{
   digitalWrite(buzzerPin,HIGH);
   delay(1000);
   digitalWrite(buzzerPin,LOW);
   delay(1000);
}

8) Open and close buzzer with button

Objective:  Getting sound from buzzer according to button status

Materials: 

  • Ardunio Uno
  • Buzzer
  • Press button
  • 1K Ohm Resistor (for button)
  • Breadboard
  • Jumper cables

When controlling the buzzer module with the button, we will apply 2 different conditions in this example. In the first case, when the button is pressed, the buzzer sounds out of the buzzer amd if button is not pressed sound won't be get. In the second case when the button is pressed, the buzzer starts to sound and when we press it again, there is no sound from the buzzer. In fact, we did this in the fourth activity. Here the buzzer will be used instead of LED.

First case: When button is pressed, the buzzer sounds and when it is not pressed buzzer does not sound. Code is here:

int buttonPin=2;
int buzzerPin=4;
int buttonSt=0;

void setup(){
    pinMode(buzzerPin,OUTPUT); //Set buzzerpin output
    pinMode(buttonPin,INPUT); // Set buttonPin input
}
void loop(){ 
    buttonSt=digitalRead(buttonPin); // Reading value from button
    if(buttonSt==HIGH)
    digitalWrite(buzzerPin,HIGH);
    else 
    digitalWrite(buzzerPin,LOW);
}

Second Case: When button is pressed, buzzer starts sound and when pressed again buzzer stops sound. Code is here:

int buttonPin=2;
int buzzerPin=4;
int buttonSt=0;
int buzzerSt=0;
void setup(){
    pinMode(buzzerPin,OUTPUT); //buzzer pinimizi çıkıs ayarladık
    pinMode(buttonPin,INPUT); // buton pinimizi giriş ayarladık
 
}
void loop(){
    
    buttonSt=digitalRead(buttonPin); // butondan değeri okuyoruz 
    if(buttonSt==HIGH){
       if(buzzerSt==0)
       {  
       buzzerSt=1;
       }
       else
       {
       buzzerSt=0; 
       }
       delay(250); //Putting a little delay to prevent rapid transitions during press button 
}; if(buzzerSt==1) digitalWrite(buzzerPin,HIGH); else digitalWrite(buzzerPin,LOW); }

To be continued...

454 views
COMMENTS