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

On a raspberry pi, write an assembler program that will calculate the factorial of an integer...
On a raspberry pi, write an assembler program that will calculate the factorial of an integer value inputted by the user. the program should detect if an overflow occurs. If no overflow occurred, then it should print out the value of the factorial. Otherwise print out a message indicating that an overflow occurred.
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:
Write a program in C that computes the area of a circle (Area = pi *...
Write a program in C that computes the area of a circle (Area = pi * r2) and the volume of a sphere (Volume = 4/3 * pi * r3). Both formulas use r which is the radius. Declare a float variable pi = 3.14159. Get the value of r from the keyboard and store it in a float variable. Display both the area of the circle and the volume of the sphere.
Assignment (C language) We will simulate the status of 8 LEDs that are connected to a...
Assignment (C language) We will simulate the status of 8 LEDs that are connected to a microcontroller. Assume that the state of each LED (ON or OFF) is determined by each of the bits (1 or 0) in an 8-bit register (high-speed memory). Declare a char variable called led_reg and initialize it to 0. Assume that the least-significant bit (lsb) controls LED#0 and the most-significant bit (msb) controls LED#7. In the main function, build and present a menu to the...
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)....
Q2. Write an 8051 C program that uses 3 temperature sensors (TS1, TS2, TS3),   3 LEDs...
Q2. Write an 8051 C program that uses 3 temperature sensors (TS1, TS2, TS3),   3 LEDs (GreenLED, BlueLED and RedLED) and 1 buzzer (BUZZ). Monitor the temperature sensors such that whenever any of the sensor goes high, the buzzer will produce a sound and at the same time, its corresponding LED blinks 3 times. You may use any of the I/O Port Pins.     SOLUTION: C PROGRAM
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT