Arduino Code
Arduino Code
int LED=13;
void setup() {
// put your setup code here, to run once:
pinMode(LED,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED,LOW);
delay(100);
digitalWrite(LED,HIGH);
delay(100);
}
LED Using PWM
// Define the analog input pin for the sensor
int sensorPin = A0;
void setup() {
// Set the LED pin as OUTPUT
pinMode(LED, OUTPUT);
}
void loop() {
// Read the analog value from the sensor
sensorValue = analogRead(sensorPin);
// The setup function runs once when you press reset or power the
board
void setup() {
// Set LEDOut and ButtonIn pins as output and input, respectively
pinMode(LEDOut, OUTPUT);
pinMode(ButtonIn, INPUT);
}
// Delay for 1 second (1000 milliseconds)
delay(1000);
void setup() {
// Start serial communication
Serial.begin(9600);
// Print a message to the serial monitor
Serial.println(F("DHTxx test!"));
/* IMPORTANT
______________________________________________________
In Arduino programming, the F() macro is used to store string
literals (constant strings) in flash memory (program memory) instead
of RAM.
Without the F() macro, the string would be stored in RAM, which is
more limited than flash memory on most Arduino boards.
______________________________________________________
*/
// Initialize the DHT sensor
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Read humidity and temperature values from the DHT sensor
// Reading temperature or humidity takes about 250 milliseconds!
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
// Check if any of the sensor readings are NaN (Not a Number).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Calculate heat index based on temperature and humidity
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
// Print the sensor readings and calculated heat index to the serial
monitor
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));}
5_Clock
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
void setup() {
Serial.begin(115200);
void loop() {
// Update time values based on millis()
MilSeconds = millis() % 1000;//Just used to print time one time only
every 1 Second
Second = millis() / 1000;
Min = millis() / 60000;
Hours = millis() / 360000;
void setup() {
Serial.begin(115200);
pinMode(sw, INPUT);
}
display.print(" ");
if(Hours % 24<10){ display.print("0");}
display.print(Hours % 24, 1);
display.print(":");
if(Min % 60<10){ display.print("0");}
display.print(Min % 60, 1);
display.print(":");
if(Sec % 60<10){ display.print("0");}
display.print(Sec % 60, 1);
display.display();
Serial.print(Hours % 24);
Serial.print(":");
Serial.print(Min % 60);
Serial.print(":");
Serial.println(Sec % 60);
}
}
void loop() {
imsec = millis()%1000;
switch (i) {
case 0:
// Display initial time
printTime(imsec,0, 0, 0);
value = digitalRead(sw);
if (value == 0) {
delay(200);
isec = millis() ;
imin = millis() ;
ihours = millis() ;
i++;
}
break;
case 1:
// Running stopwatch
sec = millis()-isec ;
min = millis()-imin ;
hours = millis()-ihours ;
printTime(imsec,sec / 1000, (min / 1000) / 60, (hours / 1000) /
3600);
value = digitalRead(sw);
if (value == 0) {
delay(200);
i++;
}
break;
case 2:
// Pause display
printTime(imsec,sec / 1000, (min / 1000) / 60, (hours / 1000) /
3600);
value = digitalRead(sw);
if (value == 0) {
delay(200);
i = 0;
}
break;
}
}
7_Resistance_Meater
int v1,v2;
float vx,vy;
float I;
float Resistance;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Resistance =0;
for(int x=0;x<50;x++){
v1=analogRead(A0);
v2=analogRead(A1);
vx=5.0-(v1*5.0/1023.0);// fixed resistor to measuer current
vy=(v1-v2)*5.0/1023.0; // variable resistor
I=vx/1000.0;
Resistance+=(vy/I);
}
Serial.print("Resistance = ");
Serial.print(Resistance/50.0);
Serial.println(" Ω");
delay(500);
}
8_Frequancy_Counter_Simple
// Define the pin for frequency measurement
const int FreqPin = 3;
void setup() {
// Set up the pin mode for input
pinMode(FreqPin, INPUT);
// Initialize variables
Cnt = 0; // Counter for frequency measurement
Prevstate = false; // Previous state of the input pin
CurrentState = 0; // Current state of the input pin
T = micros(); // Initialize time for one-second intervals
}
void loop() {
// Read the current state of the input pin
CurrentState = digitalRead(FreqPin);
// Reset the count and update the time for the next second
Cnt = 0;
T = micros();
}
}
9_Frequency_Counter_Final
// Frequency Counter using Arduino
// D4 and D3 generate a 1 Hz and 250 Hz square wave, respectively.
// Input frequency to be measured is connected to pin D5.
void setup() {
// Initialize serial communication
Serial.begin(115200);
void setup() {
pinMode(AIn, INPUT); // Set the analog pin as input
pinMode(DPin, OUTPUT); // Set the digital pin as output for
charging
digitalWrite(DPin, LOW); // Set the charging pin low initially
void loop() {
Vo = 0.0; // Initialize voltage variable
void setup() {
// Set pin modes and initial values
pinMode(DigPin, OUTPUT); // Digital pin for charging the capacitor
pinMode(AngPin, INPUT); // Analog pin for reading the voltage
digitalWrite(DigPin, LOW); // Set charging pin low initially
Ce = 0.0; // Initialize initial offset capacitance
Rext = 10; // Set external resistor value
Serial.begin(115200); // Initialize serial communication
delay(1000); // Delay for 1 second
}
void loop() {
digitalWrite(DigPin, HIGH); // Start charging the capacitor
Tm = micros(); // Record the current time in
microseconds
// Wait until the voltage across the capacitor reaches 3.16 volts
while (analogRead(AngPin) < 646) {}