MicroSD Card Module

Image.png
Image.png
COMPONENTS:
-Arduino UNO
-MicroSD card module

CODE:
/*
  SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI – pin 11
** MISO – pin 12
** CLK – pin 13
** CS – pin 4 (for MKRZero SD: SDCARD_SS_PIN)
created   Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.print(“Initializing SD card…”);
  if (!SD.begin(4)) {
    Serial.println(“initialization failed!”);
    return;
  }
  Serial.println(“initialization done.”);
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open(“test.txt”, FILE_WRITE);
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print(“Writing to test.txt…”);
    myFile.println(“testing 1, 2, 3.”);
    // close the file:
    myFile.close();
    Serial.println(“done.”);
  } else {
    // if the file didn’t open, print an error:
    Serial.println(“error opening test.txt”);
  }
  // re-open the file for reading:
  myFile = SD.open(“test.txt”);
  if (myFile) {
    Serial.println(“test.txt:”);
    // read from the file until there’s nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn’t open, print an error:
    Serial.println(“error opening test.txt”);
  }
}
void loop() {
  // nothing happens after setup
}

Keypad

COMPONENTS:
-Arduino Uno
-Keypad

Open Library Manager and install Keypad in there

CODE:
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
  {‘1’, ‘2’, ‘3’, ‘A’},
  {‘4’, ‘5’, ‘6’, ‘B’},
  {‘7’, ‘8’, ‘9’, ‘C’},
  {‘*’, ‘0’, ‘#’, ‘D’}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup(){
  Serial.begin(9600);
}
void loop(){
  char customKey = customKeypad.getKey();
  if (customKey){
    Serial.println(customKey);
  }
}

Temperature Sensor

Image.png
Image.jpg
Image.png
COMPONENTS:
-Arduino Uno
-DS18B20 Temperature Sensor
-Resistor 4.7k
-LCD I2C
CODE:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD I2C address
LiquidCrystal_I2C lcd(0x27, 20, 4);
/********************************************************************/
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
/********************************************************************/
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
/********************************************************************/
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
/********************************************************************/
void setup(void)
{
  lcd.init();
  lcd.backlight();
  // start serial port
  Serial.begin(9600);
  Serial.println(“Dallas Temperature IC Control Library Demo”);
  // Start up the library
  sensors.begin();
}
void loop(void)
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  /********************************************************************/
  sensors.requestTemperatures(); // Send the command to get temperature readings
  /********************************************************************/
  Serial.print(“TEMP: “);
  Serial.print(sensors.getTempCByIndex(0)); // Why “byIndex”?
  Serial.print(‘\n’);
  // You can have more than one DS18B20 on the same bus.
  // 0 refers to the first IC on the wire
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(“TEMP:”);
  lcd.print(sensors.getTempCByIndex(0));
  lcd.print(“C”);
  delay(1000);
}

Soil Moisture Sensor

Image.png
Image.jpg
Image.png
COMPONENTS:
-Arduino Uno
-Soil Moisture Sensor
-LCD I2C

CODE:
//I2C LCD:
#include <Wire.h> // Comes with Arduino IDE
#include <LiquidCrystal_I2C.h>
// Set the LCD I2C address
LiquidCrystal_I2C lcd(0x27, 20, 4);
// These constants won’t change. They’re used to give names
// to the pins used:
const int analogInPin = A0; // Analog input pin that the Sensor is attached to
int sensorValue = 0; // value read from the Soil Moisture
void setup() {
  lcd.init();
  lcd.backlight();
  delay(100);
}
void loop() {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(“Soil Moisture:”);// print the results to the LCD Display:
  sensorValue = analogRead(analogInPin);// read the analog in value:
  lcd.setCursor(0, 1);
  lcd.print(sensorValue);
  // wait 300 milliseconds before the next loop for the
  //analog-to-digital converter to settle after the last reading:
  delay(300);
}

Server Life Tester

Image.png

Image.png
Image.png

COMPONENTS:
-Arduino Uno
-Ethernet ENC28J60
-LCD I2C

CABLING:
ENC28J60 > Arduino
GND -> GND pin
VCC -> 5V pin
CS -> pin 10
SI -> pin 11
SO -> pin 12
SCK -> pin 13

Download and install Ethercard library from https://github.com/jcw/ethercard

CODE:
#include <avr/pgmspace.h>
#include <EtherCard.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD I2C address
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define CS_PIN        10
#define pinToToggle   9
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
// myip,gwip,mask,dns Only needed if using a Static IP
//static byte myip[]  = { 192,168,2,50 };
//static byte gwip[]  = { 192,168,2,1 };
//static byte mask[]  = { 255,255,255,0 };
//static byte dns[]   = { 8,8,8,8 };
byte Ethernet::buffer[700];
static int32_t timer;
static int polling;
//change these to the IPs that you want to ping
const char srv1[]  PROGMEM = “10.0.10.1”;
const char srv2[]  PROGMEM = “8.8.8.8”;
// Make sure this list matches the above list
const char* const servers[] PROGMEM = {srv1,srv2};
#define NUMSRVRS (sizeof(servers) / sizeof(servers[0]))
#define MAXCYCLES 3
#define PING_DELAY 2000000
static int cycles;
char sbuffer[30];
static int i;
float ms;
long interval              = 500;
unsigned long currentTime  = 0;
unsigned long previousTime = 0;
void blink() {
  currentTime = millis();
  if(currentTime – previousTime > interval) {
    previousTime = currentTime;
    digitalWrite(pinToToggle, !digitalRead(pinToToggle));
  }
}
static void gotPinged (byte* ptr) {
  ether.printIp(“>>> ping from: “, ptr);
}
void setup () {
  pinMode(pinToToggle,OUTPUT);
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
// Uncomment to wait till serial port is ready
//  while (!Serial) { }
  Serial.println(F(“Ping Servers\n”));
  if (!ether.begin(sizeof Ethernet::buffer, mymac, CS_PIN))
    Serial.println(F(“Failed to access Ethernet controller\n”));
  strcpy_P(sbuffer, (char*)pgm_read_word(&(servers[0])));
  ether.parseIp(ether.hisip,sbuffer);
// Comment these two lines and uncomment staticSetup
// to use static vs DHCP IP address
  if (!ether.dhcpSetup())
    Serial.println(F(“DHCP failed\n”));
//  if (!ether.staticSetup(myip, gwip, dns, mask))
//    Serial.println(F(“Failed to set Static IP”));
  while (ether.clientWaitingGw())
    ether.packetLoop(ether.packetReceive());
  Serial.println(“Gateway found”);
  ether.printIp(“IP:  “,     ether.myip);
  ether.printIp(“GW:  “,     ether.gwip);
  ether.printIp(“Netmask: “, ether.netmask);
  ether.printIp(“DNS IP: “,  ether.dnsip);
  Serial.println();
  ether.registerPingCallback(gotPinged);
  timer   = -9999999;
  polling = 0;
  i       = NUMSRVRS;
  cycles  = 0;
  digitalWrite(pinToToggle, !digitalRead(pinToToggle));
  delay(2000);
  digitalWrite(pinToToggle, !digitalRead(pinToToggle));
}
void loop () {
// Uncomment to change to no delay
// if ( ( (micros() – timer) >= PING_DELAY) || !polling  ) {
//Comment this next line, if the above line is uncommented
if ((micros() – timer) >= PING_DELAY) {
   if (polling) {
     ether.printIp(“\n”,ether.hisip);
     Serial.println(F(“Timeout\n”));
     lcd.setCursor(0, 1);
     lcd.print(“Timeout”);
     if (cycles >= MAXCYCLES) while (1) blink();
   }
   else {
     polling = 1;
     lcd.clear();
     lcd.setCursor(0, 0);
     ++i;
     if (i >= NUMSRVRS) i = 0;
     strcpy_P(sbuffer, (char*)pgm_read_word(&(servers[i])));
     lcd.print(sbuffer);
     ether.parseIp(ether.hisip,sbuffer);
     ether.printIp(“Pinging: “, ether.hisip);
   }
   cycles += 1;
   ether.clientIcmpRequest(ether.hisip);
   timer = micros();
  }
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);
  if (len > 0 && ether.packetLoopIcmpCheckReply(ether.hisip)) {
    Serial.print(”  “);
    ms = (micros() – timer) * 0.001;
    Serial.print(ms, 3);
    Serial.println(” ms\n”);
    lcd.setCursor(0, 1);
    lcd.print(ms);
    lcd.print(” ms”);
    polling   = 0;
    cycles    = 0;
  }
}

Joystick with 2 Servos

Image.png
Image.png
Image.png
COMPONENTS:
-Arduino Uno
-Joystick
-2x Servos

CODE:
#include “Servo.h”;
Servo servo1;
Servo servo2;
int x_key = A0;
int y_key = A1;
int x_pos;
int y_pos;
int servo1_pin = 11;
int servo2_pin = 12;
int initial_position = 90;
int initial_position1 = 90;
void setup ( ) {
  Serial.begin (9600) ;
  servo1.attach (servo1_pin ) ;
  servo2.attach (servo2_pin ) ;
  servo1.write (initial_position);
  servo2.write (initial_position1);
  pinMode (x_key, INPUT) ;
  pinMode (y_key, INPUT) ;
}
void loop ( ) {
  x_pos = analogRead (x_key) ;
  y_pos = analogRead (y_key) ;
  if (x_pos < 300) {
    if (initial_position < 10) { } else {
      initial_position = initial_position – 20;
      servo1.write ( initial_position ) ;
      delay (100) ;
    }
  } if (x_pos > 700) {
    if (initial_position > 180)
    {
    }
    else {
      initial_position = initial_position + 20;
      servo1.write ( initial_position ) ;
      delay (100) ;
    }
  }
  if (y_pos < 300) {
    if (initial_position1 < 10) { } else {
      initial_position1 = initial_position1 – 20;
      servo2.write ( initial_position1 ) ;
      delay (100) ;
    }
  } if (y_pos > 700) {
    if (initial_position1 > 180)
    {
    }
    else {
      initial_position1 = initial_position1 + 20;
      servo2.write ( initial_position1 ) ;
      delay (100) ;
    }
  }
}

Gyro with Accelerometer Sensor

Image.png
Image.png
COMPONENTS:
-Arduino Uno
-MPU6050 3 Axis Gyro with Accelerometer

Download and extract i2cdevlib library from https://github.com/jrowberg/i2cdevlib
copy i2cdevlib-master/Arduino/I2Cdev into C:\Users\user1\Documents\Arduino\libraries
copy i2cdevlib-master/Arduino/MPU6050 into C:\Users\user1\Documents\Arduino\libraries

CODE:
#include “Wire.h” // This library allows you to communicate with I2C devices.
const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69.
int16_t accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer raw data
int16_t gyro_x, gyro_y, gyro_z; // variables for gyro raw data
int16_t temperature; // variables for temperature data
char tmp_str[7]; // temporary variable used in convert function
char* convert_int16_to_str(int16_t i) { // converts int16 to string. Moreover, resulting strings will have the same length in the debug monitor.
  sprintf(tmp_str, “%6d”, i);
  return tmp_str;
}
void setup() {
  Serial.begin(9600);
  Wire.begin();
  Wire.beginTransmission(MPU_ADDR); // Begins a transmission to the I2C slave (GY-521 board)
  Wire.write(0x6B); // PWR_MGMT_1 register
  Wire.write(0); // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
}
void loop() {
  Wire.beginTransmission(MPU_ADDR);
  Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) [MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2, p.40]
  Wire.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a result, the connection is kept active.
  Wire.requestFrom(MPU_ADDR, 7*2, true); // request a total of 7*2=14 registers
  // “Wire.read()<<8 | Wire.read();” means two registers are read and stored in the same variable
  accelerometer_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
  accelerometer_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
  accelerometer_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
  temperature = Wire.read()<<8 | Wire.read(); // reading registers: 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
  gyro_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
  gyro_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
  gyro_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)
  // print out data
  Serial.print(“aX = “); Serial.print(convert_int16_to_str(accelerometer_x));
  Serial.print(” | aY = “); Serial.print(convert_int16_to_str(accelerometer_y));
  Serial.print(” | aZ = “); Serial.print(convert_int16_to_str(accelerometer_z));
  // the following equation was taken from the documentation [MPU-6000/MPU-6050 Register Map and Description, p.30]
  Serial.print(” | tmp = “); Serial.print(temperature/340.00+36.53);
  Serial.print(” | gX = “); Serial.print(convert_int16_to_str(gyro_x));
  Serial.print(” | gY = “); Serial.print(convert_int16_to_str(gyro_y));
  Serial.print(” | gZ = “); Serial.print(convert_int16_to_str(gyro_z));
  Serial.println();
  // delay
  delay(1000);
}