Arduino

Reference Material

Some reference material:

Your First Program

Target: Build a leg with the Arduino build in pin 13.

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

To avoid using the delay function you can use the following code: https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay

// constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Different Sensor Integration Arduino - Codes

Program - Push Button

Illustration is on : https://www.arduino.cc/en/Tutorial/BuiltInExamples/DigitalReadSerial

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState = digitalRead(pushButton);
  // print out the state of the button:
  Serial.println(buttonState);
  delay(1);        // delay in between reads for stability
}

We can combine with First program and pushbutton

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
const int ledPin =  LED_BUILTIN;
int ledState = LOW; 
unsigned long previousMillis = 0; 
const long interval = 1000;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
  pinMode(ledPin, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  unsigned long currentMillis = millis();
  // read the input pin:
  int buttonState = digitalRead(pushButton);
  // print out the state of the button:
  Serial.println(buttonState);
  delay(1);        // delay in between reads for stability
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

PIR Motion Sensor

Reference: https://www.makerguides.com/hc-sr501-arduino-tutorial/

GPS Sensor

Module Reference: GP-GPS6MV2

Get the NEMA Format GPS

/*
 * MARC-ANTOINE LEMAIRE - GPS Raw Data: NMEA Sentences
 */
 
#include <SoftwareSerial.h>

// The serial connection to the GPS module
SoftwareSerial ss(4, 3);

void setup(){
  Serial.begin(9600);
  ss.begin(9600);
}

void loop(){
  while (ss.available() > 0){
    // get the byte data from the GPS
    byte gpsData = ss.read();
    Serial.write(gpsData);
  }
}

// Output interesting:
// $GNGGA,162839.000,5037.76633,N,00449.22393,E,1,11,0.9,148.3,M,45.9,M,,*4C
// GGNA, Time, Lat, N, Long, E, 1=Fix Quality, NumSatellite, Dilluation, altitude, 

Use Tiny GPS++ Library

/*
 * MARC-ANTOINE LEMAIRE
 */
 
#include <TinyGPS++.h>
#include <SoftwareSerial.h>

static const int RXPin = 4, TXPin = 3;
// Connect TXPin3 Arduino to RX GPS
// Connect RXPin4 Arduino to TX GPS
static const uint32_t GPSBaud = 9600;

// The TinyGPS++ object
TinyGPSPlus gps;

// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

void setup(){
  Serial.begin(9600);
  ss.begin(GPSBaud);
}

void loop(){
  // This sketch displays information every time a new sentence is correctly encoded.
  while (ss.available() > 0){
    gps.encode(ss.read());
    if (gps.location.isUpdated()){
      Serial.print("Latitude= "); 
      Serial.print(gps.location.lat(), 6);
      Serial.print(" Longitude= "); 
      Serial.print(gps.location.lng(), 6);
      // Altitude in meters (double)
      Serial.print(" Altitude in meters = "); 
      Serial.print(gps.altitude.meters()); 
       // Number of satellites in use (u32)
      Serial.print(" Number of satellites in use = "); 
      Serial.print(gps.satellites.value()); 
      // Raw time in HHMMSSCC format (u32)
      Serial.print(" Raw time in HHMMSSCC = "); 
      Serial.println(gps.time.value()); 
      delay(5000);
      // We display all the 5 sec the position.
    }
  }

}

Emettor - Receptor 433 MHz RF module - Code

Temperature and Humidity Sensor

DHT11 Sensor (+- 2 euro /piece)

#include <dht.h>

dht DHT;

#define DHT11_PIN 7

void setup(){
  Serial.begin(9600);
}

void loop(){
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  delay(5000);
}

// DHT.temperature -> in degree 22.00
// DHT.humidity -> in 36.00

Reference URL:

Last updated