Lab 02 - GPIO operations

Purpose

  • Get familiar with operations related to Arduino pins
  • Get familiar with the basic concepts of programming

Keywords

  • pinMode, digitalWrite, HIGH, LOW, LED
  • for, if, while, vector

Programming concepts

Variables

Variables are ways in which we store values ​​of certain types.

variabile.c
#include <stdio.h>
 
int main(void) 
{
      int variable_number = 10;
      char variable_character = 'c';
      float variable_fractional_number = 10.25;
      return 0;
}

Arrays

Arrays are complex variables of a certain size that can store a list of values. Example:

variabile.c
#include <stdio.h>
 
int main(void) 
{
      int arr1[6];
      int arr2[] = { 1,2,3,4,5,6};
      char arr3[] = {'a', 'b', 'c', 'd'};
      char arr4[6] = "hello";
      return 0;
}

If - Else

The if statement is a conditional sentence used to execute instructions based on certain conditions. The if-else construction translates to “if - otherwise”:

if_else.c
#include <stdio.h>
 
int main(void) 
{
      int a = 10;
      if (a == 10) { //if a is equal to 10
          // do operations when a is 10
      } else { // Else
          // do operations when a is different from 10
      }
      return 0;
}

For

The for structure is a repetitive structure dependent on a condition. Usage:

for.c
#include <stdio.h>
 
int main(void) 
{
      //for(initialization, condition, operation) {}
      int i;
      for (i = 0; i <= 10; i++) {
          printf("%d ", i); // show i;
      } 
      return 0;
 
      // The code above will display: 0 1 2 3 4 5 6 7 8 9 10
}

Iterate an array

Accessing a certain value in an array is done by the index (position) of the value in the array.

vector.c
#include <stdio.h>
 
int main(void) 
{
      int i;
      int arr[] = {1,2,3,4,5,6,7,8,9};
      int sum = 0;
 
      printf("%d\n", arr[2]); // show 3
 
      for (i = 0; i < 9; i++) { // the positions in arrays start from 0!!
 
          sum = sum + arr[i]; // the sum of all the elements in the array
      } 
 
      return 0;
}

Functions

The functions divide complex tasks into small pieces that are easier to understand and to program. These can be reused for other occasions, instead of being rewritten from scratch. Also, the functions are useful for hiding the operating details of certain parts of the program, helping in its working mode. Using functions, which represent the fundamental unit of execution of C programs, a logical division of large and complex programs is obtained.

vector.c
#include <stdio.h>
 
void show(int number) {
    printf("%d\n", number);
}
 
int main(void) 
{
    int x = 10;
 
    show(10); // it will show 10
 
    return 0;
}

Arduino pins operations

To use the pins of the Arduino, we need to set the way we want to use them (INPUT/OUTPUT). Example:

void setup() {
    pinMode(MY_PIN_IN, INPUT);
    pinMode(MY_PIN_OUT, OUTPUT);
}
void loop() {
}

If we set a pin to OUTPUT mode, we can now output voltage to this pin using digitalWrite(). Example:

void setup() {
    pinMode(MY_PIN_IN, INPUT);
    pinMode(MY_PIN_OUT, OUTPUT);
}
void loop() {
    digitalWrite(MY_PIN_OUT, HIGH); //on this pin we now have 5V
    delay(3000); // wait 3 seconds
    digitalWrite(MY_PIN_OUT, LOW); //now we have 0V on this pin
}

Exercises

All exercises will be performed on Tinkercad

  1. Use a voltmeter to measure the voltage on GPIO pin number 3. Set the pin to HIGH and LOW and notice how the values change.
  2. Using an ARDUINO, a LED and a 220 ohm RESISTOR, turn on the LED (don't use GPIO pins), then measure the current and the voltage on that circuit.
  3. Using an ARDUINO, a LED and a 220 ohm RESISTOR, make the LED blink. Solve this exercise in two ways.
  4. Simulate a car traffic light and a pedestrian traffic light to be synchronized. The traffic lights will be implemented using LEDs and will iterate an array. Solve this exercise in two ways.
  5. Connect to an ARDUINO a 7 SEGMENT DISPLAY and using helper functions, display each number on the display. Solve this exercise in two ways: use a common anode 7 segment display, then a common cathode one.
  6. Using the 7 segment display implement a counter that displays the numbers from 0 to 9.

100

  1. Connect 5 LEDs and control them so that each one flashes one at a time: first once, second twice, etc.
  2. Use an LED to signal a word in morse code.
  3. Use a buzzer to do the same.
info2/labs/lab2.txt · Last modified: 2020/03/10 12:32 by ioana_maria.culic
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0