Question

In: Computer Science

Write a C program to blink two LEDs connected to Raspberry pi. One must blink at...

Write a C program to blink two LEDs connected to Raspberry pi. One must blink at a rate of 1 Hz and the other at a rate of 2 HZ.

Solutions

Expert Solution

Here our task is to blink 2 Led at rate of 1 KHz and 2 KHz

In order program this in raspberry pi we have to include wiring.pi libraray to Raspberry Pi .After successful installation of the library file you can code it in any available C IDE such as geany etc..

Things to consider

  • We have to define a varaible called led which stores the gpio pin number at which Led is connected.Make the Led pin as OUTPUT
  • We can implement the blink by using digital write() and delay() functions
  • total delay(on time +off time) for each led can be findout using formula

delay= 1 frequency

For first led

delay=1 1000 Hz = 1ms

so on time = 0.5 ms =500 micro seconds

off time =0.5 ms =500 micro seconds

For led 2

delay=1 2000 Hz = 0.5ms

so on time = 0.25 ms =250 micro seconds

off time =0.25ms =250 micro seconds

CODE

(please read all comments for better understanding of the program)

I am also attching a text version of the code in case you need to copy paste

#include <wiringPi.h> //including wiringpi file from library
#include <stdio.h>

#define Led1 0 // led which blink at 1 KHz rate at pin 0
#define Led2 1 // led which blink at 2 KHz rate at pin 1


int main() {

pinMode(Led, OUTPUT);
while(1) { // a loop having delay of 1 millisecond (1 millisec=1000 micro sec)
  
digitalWrite(Led1, HIGH); //turning on led 1 at beginning of loop
digitalWrite(Led2, HIGH); //turning on led 2 at beginning of loop
  
delayMicroseconds(250);   //delay of 250 microseconds         
digitalWrite(Led2, LOW); //led2 off (2khz led)
delayMicroseconds(250);
digitalWrite(Led1, LOW); //turning on off led 1 ( so it blink 1 times in loop (1 ms))
digitalWrite(Led2, HIGH); //led 2 on again
delayMicroseconds(250);
digitalWrite(Led2, LOW); //led 2 off (so that it blink 2 times in a loop(1 ms)
delayMicroseconds(250);
  
}
return 0;
}


Related Solutions

Write a Python code that when the temperature of the CPU in the raspberry pi exceeds...
Write a Python code that when the temperature of the CPU in the raspberry pi exceeds 40 degrees Celsius you receive an email.
The literature review of raspberry pi, also after that write Component that is technically used and...
The literature review of raspberry pi, also after that write Component that is technically used and Drawback of the subject what u choose brief of the subject: A component that is technically used: Drawback:
Using C++ Write One one single program with two or more functioncallsWrite a C++...
Using C++ Write One one single program with two or more function callsWrite a C++ function, smallest Index, that takes as parameters an int array and its size and returns the index of the smallest element in the array. Also the program should test the function.Write another function that prompts the user to input a string and outputs the string in uppercase letters. You must use a character array to store the string.
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
For the mspfr6989 write program #2 1(b) Write a program that will make LED1 blink 5...
For the mspfr6989 write program #2 1(b) Write a program that will make LED1 blink 5 times when S1 is pressed, and then stop. Program #2 – Using both S1 and S2 1 Write a program like 1(b) above, but the LED1 will blink until S2 is pressed. 2 Write a program that simulates a motor coming up to speed. When S1 is pressed, LED1 blinks at 50% duty cycle for 10 cycles (indicating a motor coming up to speed)....
Submit a c++ file: 2. Write a program that defines the named constant PI, const double...
Submit a c++ file: 2. Write a program that defines the named constant PI, const double PI = 3.14159;, which stores the value of p. The program should use PI and the functions listed in Table 6-1 to accomplish the following: a. Output the value of Pi Output the value of Pi. b. Prompt the user to input the value of a double variable r, which stores the radius of a sphere. The program then outputs the following: i.   The...
1. An LED is connected to PORTB.5 of ATmega328 microcontroller. Write a C Program that toggles...
1. An LED is connected to PORTB.5 of ATmega328 microcontroller. Write a C Program that toggles LED after 0.5 Seconds. Assume XTAL = 16MHz. To generate this delay use Timer 1 CTC (Clear Timer on Compare match) mode Programming. 2. Write a program to generate a square wave of frequency of 250 Hz with 50% duty cycle on PORTB.5. Assume XTAL = 16MHz. Use Timer 2 Normal Mode Programming. 3. Write a program using 16-bit timer to generate a square...
IN C++ Write a program to play the Card Guessing Game. Your program must give the...
IN C++ Write a program to play the Card Guessing Game. Your program must give the user the following choices: - Guess only the face value of the card. -Guess only the suit of the card. -Guess both the face value and suit of the card. Before the start of the game, create a deck of cards. Before each guess, use the function random_shuffle to randomly shuffle the deck.
1. IN C LANGUAGE: Write a code that makes the MSP430 blink the red LED and...
1. IN C LANGUAGE: Write a code that makes the MSP430 blink the red LED and the green LED at the same time. The two LEDs should be on at the same time and then off at the same time
Must be done in C Write a C program found out the maximum value between the...
Must be done in C Write a C program found out the maximum value between the three numbers? Show steps with comments or else it will be flagged.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT