Tuesday, August 9, 2016

Create knight rider circuit using arduino MEGA 2560

Last time I discussed about Arduino Mega 2560 micro controller board . Now I'm going to discuss about a simple project using Arduino Mega 2560. Let's start it.

 We want following components to make the circuit with the Arduino MEGA 2560 .

                                                                       
LED
 10 LEDs
Male to male jumper wires





One bread board














   
270 Ohms Resister



Now I consider how to create circuit using these components.

1.Connect LED's to the bread board.





2.Connect jumper wires


Male to male jumper wires are connected to each positive leads of LEDs and the other ends are connected to the Arduino Mega micro controller's digital pins according to a order given in the picture


Digital pins 2 - 11 of Arduino MEGA board used. Ground is connected to bread board through a  270 Ohm resistor. This resister controls required voltage and the current to LEDs.

Now circuit is ready. We have to implement the code 


The Mega 2560 board can be programmed with the Arduino Software(IDE). I have described about it in my earlier blog post.If u don't know anything about programming in Arduino refer my earlier blog post. 

This code will make the LEDs blink in a sequence,  using only pinMode(pinNumber,output/input) , digitalWrite(pinNumber,0/1) and delay (time in milliseconds) functions.

 We can use a pin as a input or output. pinMode() is used to define how we are going to use a pin.So we use it as pinMode(pinNumber,OUTPUT/INPUT) .
There are digital pins and analog pins in Arduino Mega board.So we have to define what type of pins  are going to used. To define that we can use digitalWrite() or analogWrite() functions.

And also we can assign value of output of the considered pin using digitalWrite() or analogWrite() functions. We use delay() function to pause execution of the code between desired time period.

Code

int i;
void setup(){
  // put your setup code here, to run once:
        for(i=1;i<12;i++){
             pinMode(i,OUTPUT);  // define pinMode
        }    
}

void loop() {
  // put your main code here, to run repeatedly:
        for(i=1;i<9;i++){
            digitalWrite(i,1);
            digitalWrite(i+1,1);
           digitalWrite(i+2,1);
           digitalWrite(i+3,1);
           digitalWrite(i,0);
           delay(100);  
        }
  
       for(i=11;i>5;i--){
  
          digitalWrite(i,1);
          digitalWrite(i-1,1);
          digitalWrite(i-2,1);
          digitalWrite(i-3,1);
          digitalWrite(i,0);
         delay(100);
    
        }  
}

Brief introduction of the code


setup() function

The setup() function is called when the code starts. Use it to initialize variables, pin modes, start using libraries, etc. The setup function will only run once, after each power up or reset of the Arduino board.

loop() function

After creating a setup() function, which initializes and sets the initial values, the loop() function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. Use it to actively control the Arduino board.

Steps to upload your sketch

  1.  Connect your Arduino using the USB cable
  2.  Choose Tools→Board→Arduino / Genuino Mega or Mega 2560 to find your board in the  Arduino menu
  3.  Choose the correct serial port for your board
  4.  Click the Upload button
A few seconds after the upload finishes, you should see the amber (yellow) LED on the board start to blink.

After uploading the code successfully, you can see the knight rider circuit running.  






Feel free to ask any question about this project.

If you enjoyed this post, I’d be very grateful if you’d help it spread by sharing it on Facebook and Twitter. Thank you!


11 comments:

How to send Slack notification using a Python script?

 In this article,  I am focussing on sending Slack notifications periodically based on the records in the database. Suppose we have to monit...