Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
2 views50 pages

Embedded System Coding

The document contains various Arduino code snippets for controlling switches, LEDs, ADC, Bluetooth, buzzers, DAC waveforms, DC motor speed control, and EEPROM operations. Each section includes setup and loop functions to manage input and output operations for different hardware components. The code demonstrates how to read switch states, control LEDs, generate waveforms using DAC, and manage motor speed based on switch inputs.

Uploaded by

IG VIPER YT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views50 pages

Embedded System Coding

The document contains various Arduino code snippets for controlling switches, LEDs, ADC, Bluetooth, buzzers, DAC waveforms, DC motor speed control, and EEPROM operations. Each section includes setup and loop functions to manage input and output operations for different hardware components. The code demonstrates how to read switch states, control LEDs, generate waveforms using DAC, and manage motor speed based on switch inputs.

Uploaded by

IG VIPER YT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

5_SWITCH_TEST_LOGIC

int SW_1 = 35;


int SW_2 = 34;
int SW_3 = 33;
int SW_4 = 32;
int SW_5 = 39;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(SW_1, INPUT_PULLUP);
pinMode(SW_2, INPUT_PULLUP);
pinMode(SW_3, INPUT_PULLUP);
pinMode(SW_4, INPUT_PULLUP);
pinMode(SW_5, INPUT_PULLUP);
}

void loop() {
// put your main code here, to run repeatedly:
int switch_1 = digitalRead(SW_1);
int switch_2 = digitalRead(SW_2);
int switch_3 = digitalRead(SW_3);
int switch_4 = digitalRead(SW_4);
int switch_5 = digitalRead(SW_5);

if (switch_1 == 0)
{
delay(500);
Serial.println("Switch_1 is pressed");
}
if (switch_2 == 0)
{
delay(500);
Serial.println("Switch_2 is pressed");
}
if (switch_3 == 0)
{
delay(500);
Serial.println("Switch_3 is pressed");
}
if (switch_4 == 0)
{
delay(500);
Serial.println("Switch_4 is pressed");
}
if (switch_5 == 0)
{
delay(500);
Serial.println("Switch_5 is pressed");
}
}

8 BIT_LED_LOGIC

int SW_1 = 36;


int SW_2 = 39;
int SW_3 = 34;
int SW_4 = 35;
int SW_5 = 32;
int SW_6 = 33;
int SW_7 = 25;
int SW_8 = 26;

int LEd_1 = 1;
int LEd_2 = 15;
int LEd_3 = 2;
int LEd_4 = 4;
int LEd_5 = 13;
int LEd_6 = 12;
int LEd_7 = 14;
int LEd_8 = 27;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(SW_1, INPUT);
pinMode(SW_2, INPUT);
pinMode(SW_3, INPUT);
pinMode(SW_4, INPUT);
pinMode(SW_5, INPUT);
pinMode(SW_6, INPUT);
pinMode(SW_7, INPUT);
pinMode(SW_8, INPUT);

pinMode(LEd_1, OUTPUT);
pinMode(LEd_2, OUTPUT);
pinMode(LEd_3, OUTPUT);
pinMode(LEd_4, OUTPUT);
pinMode(LEd_5, OUTPUT);
pinMode(LEd_6, OUTPUT);
pinMode(LEd_7, OUTPUT);
pinMode(LEd_8, OUTPUT);
//digitalWrite(LEd_8, LOW);
}

void loop() {
// put your main code here, to run repeatedly:
int switch_1 = digitalRead(SW_1);
int switch_2 = digitalRead(SW_2);
int switch_3 = digitalRead(SW_3);
int switch_4 = digitalRead(SW_4);
int switch_5 = digitalRead(SW_5);
int switch_6 = digitalRead(SW_6);
int switch_7 = digitalRead(SW_7);
int switch_8 = digitalRead(SW_8);
Serial.println("Hello Logsun");

delay(100);
if (switch_1 == 1)
{
digitalWrite(LEd_1, HIGH);
}
else
{
digitalWrite(LEd_1, LOW);
}
if (switch_2 == 1)
{
digitalWrite(LEd_2, HIGH);
}
else
{
digitalWrite(LEd_2, LOW);
}
if (switch_3 == 1)
{
digitalWrite(LEd_3, HIGH);
}
else
{
digitalWrite(LEd_3, LOW);
}
if (switch_4 == 1)
{
digitalWrite(LEd_4, HIGH);
}
else
{
digitalWrite(LEd_4, LOW);
}
if (switch_5 == 1)
{
digitalWrite(LEd_5, HIGH);
}
else
{
digitalWrite(LEd_5, LOW);
}
if (switch_6 == 1)
{
digitalWrite(LEd_6, HIGH);
}
else
{
digitalWrite(LEd_6, LOW);
}
if (switch_7 == 1)
{
digitalWrite(LEd_7, HIGH);
}
else
{
digitalWrite(LEd_7, LOW);
}
if (switch_8 == 1)
{
digitalWrite(LEd_8, HIGH);
}
else
{
digitalWrite(LEd_8, LOW);
}
}

ADC
#include "MCP320X.h"

// (Optional) Define model and SPI pins

#define CS_PIN 5 // ESP8266 default SPI pins

#define CLOCK_PIN 18 // Should work with any other GPIO pins, since the library does not formally

#define MOSI_PIN 23 // use SPI, but rather performs pin bit banging to emulate SPI communication.

#define MISO_PIN 19 //

#define MCP3204 4 // (Generally "#define MCP320X X", where X is the last model digit/number of
inputs)

// Create an instance of MCP320X object.

MCP320X mcp3204(MCP3204, CLOCK_PIN, MOSI_PIN, MISO_PIN, CS_PIN);

void setup() {

// Start the serial so we can see some output

Serial.begin(9600);

void loop() {

// Read from all available pins

Serial.printf("\nReading from all %i pins of the MCP320X device:\n", MCP3204);


Serial.println("==================================================>");

for(int i=0; i<MCP3204; i++){

Serial.printf("ADC%i: %i\n", i, mcp3204.readADC(i));

delay(2000);

BLUETOOTH

#include "BluetoothSerial.h"
BluetoothSerial SerialBT;

void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
if (Serial.available())
SerialBT.write(Serial.read());

if(SerialBT.available()) {
Serial.write(SerialBT.read()); }
delay(20);
}

BUZZER

int buzzer = 13;


void setup() {
// put your setup code here, to run once:
pinMode(buzzer, OUTPUT);
}
void loop() {

digitalWrite(buzzer, HIGH);
delay(1000);
digitalWrite(buzzer, LOW);
delay(1000);
// put your main code here, to run repeatedly:
}

DAC CODE
DAC_ASCENDING_SAWTOOTH
#include "MCP_DAC.h" //reference the library files

MCP4921 myDAC; //create DAC object

int LDAC = 17;

int i = 0;

void setup()

Serial.begin(9600);

myDAC.begin(5); //initialize

pinMode(LDAC, OUTPUT);

digitalWrite(LDAC, LOW);

void loop()

for (i = 0; i < 4096; i++)

{
myDAC.analogWrite(i);

// delay(10);

myDAC.analogWrite(0);

DAC_COSINE_WAVE_GENERATOR

#include "MCP_DAC.h" //reference the library files


MCP4921 myDAC; //create DAC object
int LDAC = 17;

int PWM_out = 5; //PWM output at pin5

int sample_time = 5; //ms


int N = 128; //int
int duty_cycle; //unitless

int i; // to store the samples

void setup() {

myDAC.begin(5); //initialize
pinMode(LDAC, OUTPUT);
digitalWrite(LDAC, LOW);

}
void loop() {
for (i = 0; i < N; i++){
duty_cycle = 127+127*cos(i*(2*3.14)/N);
Serial.print("Duty cycle: ");
Serial.println(duty_cycle);
myDAC.analogWrite(duty_cycle);
//delay(sample_time);
}
}
DAC_DESCENDING_SAWTOOTH

#include "MCP_DAC.h" //reference the library files


MCP4921 myDAC; //create DAC object
int LDAC = 17;
int i = 0;

void setup()
{
Serial.begin(9600);
myDAC.begin(5); //initialize
pinMode(LDAC, OUTPUT);
digitalWrite(LDAC, LOW);
}

void loop()
{

myDAC.analogWrite(0);
// delay(10);

for (i = 4095; i > 0; i--)


{
myDAC.analogWrite(i);
//delay(10);
}
}
DAC_SINE_WAVE_GENERATOR

#include "MCP_DAC.h" //reference the library files


MCP4921 myDAC; //create DAC object
int LDAC = 17;

int PWM_out = 5; //PWM output at pin5

int sample_time = 5; //ms


int N = 128; //int
int duty_cycle; //unitless

int i; // to store the samples

void setup() {

myDAC.begin(5); //initialize
pinMode(LDAC, OUTPUT);
digitalWrite(LDAC, LOW);

void loop() {
for (i = 0; i < N; i++){
duty_cycle = 127+127*sin(i*(2*3.14)/N);
Serial.print("Duty cycle: ");
Serial.println(duty_cycle);
myDAC.fastWriteA(duty_cycle);
//delay(sample_time);
}
}
DAC_SQUARE_WAVE
#include "MCP_DAC.h" //reference the library files
MCP4921 myDAC; //create DAC object
int LDAC = 17;
int i = 0;

void setup()
{
Serial.begin(9600);
myDAC.begin(5); //initialize
pinMode(LDAC, OUTPUT);
digitalWrite(LDAC, LOW);
}

void loop()
{
myDAC.analogWrite(4095);
delay(10);
myDAC.analogWrite(0);
delay(10);
}
DAC_TRIANGULAR_WAVE
#include "MCP_DAC.h" //reference the library files
MCP4921 myDAC; //create DAC object
int LDAC = 17;
int i = 0;

void setup()
{
Serial.begin(9600);
myDAC.begin(5); //initialize
pinMode(LDAC, OUTPUT);
digitalWrite(LDAC, LOW);
}

void loop()
{
for (i = 0; i < 4096; i++)
{
myDAC.analogWrite(i);
// delay(10);
}

for (i = 4095; i > 0; i--)


{
myDAC.analogWrite(i);
//delay(10);
}
}
DAC_SINE_WAVE
#include "MCP_DAC.h" //reference the library files
MCP4921 myDAC; //create DAC object
int LDAC = 17;
int i = 0;
int Array[] = { 2048,
2447,
2831,
3185,
3495,
3750,
3939,
4056,
4095,
4056,
3939,
3750,
3495,
3185,
2831,
2447,
2048,
1648,
1264,
910,
600,
345,
156,
39,
0,
39,
156,
345,
600,
910,
1264,
1648
};

void setup()
{
Serial.begin(9600);
myDAC.begin(5); //initialize
pinMode(LDAC, OUTPUT);
digitalWrite(LDAC, LOW);
}

void loop()
{
for (i = 0 ; i < 32; i++)
{
myDAC.fastWriteA(Array[i]);
}

}
DC_MOTOR_SPEED_CONTROL
int speedPin = 17;
int dir1 = 19;
int dir2 = 23;
int Switch_1 = 36;
int Switch_2 = 39;
int Switch_3 = 34;
int Switch_4 = 35;
int Switch_5 = 32;

int SW1;
int SW2;
int SW3;
int SW4;
int SW5;

int mSpeed = 0;
int dt = 500;

void setup() {
// put your setup code here, to run once:
pinMode(speedPin, OUTPUT);
pinMode(dir1, OUTPUT);
pinMode(dir2, OUTPUT);
pinMode(Switch_1, INPUT);
pinMode(Switch_2, INPUT);
pinMode(Switch_3, INPUT);
pinMode(Switch_4, INPUT);
pinMode(Switch_5, INPUT);
digitalWrite(Switch_1, HIGH);
digitalWrite(Switch_2, HIGH);
digitalWrite(Switch_3, HIGH);
digitalWrite(Switch_4, HIGH);
digitalWrite(Switch_5, HIGH);
Serial.begin(9600);

void loop() {
// put your main code here, to run repeatedly:
SW1 = digitalRead(Switch_1);
SW2 = digitalRead(Switch_2);
SW3 = digitalRead(Switch_3);
SW4 = digitalRead(Switch_4);
SW5 = digitalRead(Switch_5);
Serial.println(SW1);
Serial.println(SW2);
Serial.println(SW3);
Serial.println(SW4);
Serial.println(SW5);

Serial.print("Motor Speed ");


Serial.println(mSpeed);

if (SW4 == 0) {
Serial.println("Decrement");
mSpeed = mSpeed - 10;
delay(dt);
}
if (SW3 == 0) {
Serial.println("Increment");
mSpeed = mSpeed + 10;
delay(dt);
}
if (SW5 == 0)
{
mSpeed = 0;
Serial.println("stop");
digitalWrite(dir1, LOW);
digitalWrite(dir2, LOW);
analogWrite(speedPin, mSpeed);

}
if (SW1 == 0)
{
mSpeed = 100;
Serial.println("start");
digitalWrite(dir1, LOW);
digitalWrite(dir2, HIGH);
analogWrite(speedPin, mSpeed);
}
if (SW2 == 0)
{
mSpeed = -100;
Serial.println("reverse");
digitalWrite(dir1, HIGH);
digitalWrite(dir2, LOW);
analogWrite(speedPin, mSpeed);
}
if (mSpeed > 255) {
mSpeed = 255;
}
if (mSpeed < -255) {
mSpeed = -255;
}
if (mSpeed == 10) {
mSpeed = 100;
}
if (mSpeed == -10) {
mSpeed = -100;
}
if (mSpeed == 90 || mSpeed == 95) {
mSpeed = 0;
}
if (mSpeed == -90 || mSpeed == -95) {
mSpeed = 0;
}
if (mSpeed == 0) {
analogWrite(speedPin, 0);
}
if (mSpeed > 0) {
digitalWrite(dir1, LOW);
digitalWrite(dir2, HIGH);
analogWrite(speedPin, mSpeed);
}
if (mSpeed < 0) {
digitalWrite(dir1, HIGH);
digitalWrite(dir2, LOW);
analogWrite(speedPin, abs(mSpeed));
}
}
EEPROM 24C04
EEPROM_READ
#include <Wire.h>

#define EEPROM1_I2C_address 0x54

void setup()
{
Serial.begin(115200);
Wire.begin(13, 16); //13 SDA 16 SCL
// unsigned int address = 0;
// for (int i = 0; i <= 9; i++)
// {
// writeToEEPROM(EEPROM1_I2C_address, i, i*2);
// }
Serial.println("EEPROM 1: ");
delay(100);
for (int i = 0; i <= 9; i++)
{
Serial.print(" ");
Serial.println(readFromEEPROM(EEPROM1_I2C_address, i),DEC);
delay(700);
}
}

void loop() {}

//void writeToEEPROM(byte EEPROMAddress, byte dataAddress, byte dataValue)


//{
// Wire.beginTransmission(EEPROMAddress);
// Wire.write(dataAddress);
// Wire.write(dataValue);
// Wire.endTransmission();
// delay(5);
//}
byte readFromEEPROM(byte EEPROMAddress, byte dataAddress)
{
Wire.beginTransmission(EEPROMAddress);
Wire.write(dataAddress);
Wire.endTransmission();
delay(5);
Wire.requestFrom(EEPROMAddress, 1);
if (Wire.available()) return Wire.read();
}
EEPROM_READ_WRITE
#include <Wire.h>

#define EEPROM1_I2C_address 0x68


void setup()
{
Serial.begin(115200);
Wire.begin(21, 22); //13 SDA 16 SCL
unsigned int address = 0;
for (int i = 0; i <= 9; i++)
{
writeToEEPROM(EEPROM1_I2C_address, i, i*2);
}
Serial.println("EEPROM 1: ");
delay(100);
for (int i = 0; i <= 9; i++)
{
Serial.print(" ");
Serial.println(readFromEEPROM(EEPROM1_I2C_address, i),DEC);
delay(700);
}
}

void loop() {}

void writeToEEPROM(byte EEPROMAddress, byte dataAddress, byte dataValue)


{
Wire.beginTransmission(EEPROMAddress);
Wire.write(dataAddress);
Wire.write(dataValue);
Wire.endTransmission();
delay(5);
}
byte readFromEEPROM(byte EEPROMAddress, byte dataAddress)
{
Wire.beginTransmission(EEPROMAddress);
Wire.write(dataAddress);
Wire.endTransmission();
delay(5);
Wire.requestFrom(EEPROMAddress, 1);
if (Wire.available()) return Wire.read();
}
EEPROM_WRITE

#include <Wire.h>

#define EEPROM1_I2C_address 0x54

void setup()
{
Serial.begin(115200);
Wire.begin(13, 16); //13 SDA 16 SCL
unsigned int address = 0;
for (int i = 0; i <= 9; i++)
{
writeToEEPROM(EEPROM1_I2C_address, i, i*2);
}
Serial.println("EEPROM 1: ");
delay(100);
}

void loop() {}

void writeToEEPROM(byte EEPROMAddress, byte dataAddress, byte dataValue)


{
Wire.beginTransmission(EEPROMAddress);
Wire.write(dataAddress);
Wire.write(dataValue);
Wire.endTransmission();
delay(5);
}
ESP32_NTP_INTERNET_TIME_AND_WIFI
#include <WiFi.h>
#include "time.h"

const char* ssid = "Logsun";


const char* password = "lgs202021";

const char* ntpServer = "pool.ntp.org";


const long gmtOffset_sec = 16200;
const int daylightOffset_sec = 3600;

void setup(){
Serial.begin(115200);

// Connect to Wi-Fi
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");

// Init and get the time


configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();

//disconnect WiFi as it's no longer needed


WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}

void loop(){
delay(1000);
printLocalTime();
}

void printLocalTime(){
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
Serial.print("Day of week: ");
Serial.println(&timeinfo, "%A");
Serial.print("Month: ");
Serial.println(&timeinfo, "%B");
Serial.print("Day of Month: ");
Serial.println(&timeinfo, "%d");
Serial.print("Year: ");
Serial.println(&timeinfo, "%Y");
Serial.print("Hour: ");
Serial.println(&timeinfo, "%H");
Serial.print("Hour (12 hour format): ");
Serial.println(&timeinfo, "%I");
Serial.print("Minute: ");
Serial.println(&timeinfo, "%M");
Serial.print("Second: ");
Serial.println(&timeinfo, "%S");

Serial.println("Time variables");
char timeHour[3];
strftime(timeHour,3, "%H", &timeinfo);
Serial.println(timeHour);
char timeWeekDay[10];
strftime(timeWeekDay,10, "%A", &timeinfo);
Serial.println(timeWeekDay);
Serial.println();
}
ESP32_TOUCH
void setup() {
Serial.begin(115200);
delay(1000); // give me time to bring up serial monitor
Serial.println("ESP32 Touch Test");
}

void loop() {
Serial.print("Touch 1: ");
Serial.println(touchRead(13)); // get value of Touch 0 pin = GPIO 4
Serial.print("Touch 2: ");
Serial.println(touchRead(12));
Serial.print("Touch 3: ");
Serial.println(touchRead(14));
Serial.print("Touch 4: ");
Serial.println(touchRead(27));
delay(1000);
}
IR_SENSOR
int IRSensor = 36; // connect ir sensor module to Arduino pin 9
int LED = 2; // conect LED to Arduino pin 13
void setup()
{
Serial.begin(115200); // Init Serila at 115200 Baud
Serial.println("Serial Working"); // Test to check if serial is working or not
pinMode(IRSensor, INPUT); // IR Sensor pin INPUT
pinMode(LED, OUTPUT); // LED Pin Output
}
void loop()
{
int sensorStatus = digitalRead(IRSensor); // Set the GPIO as Input
if (sensorStatus == 1) // Check if the pin high or not
{
// if the pin is high turn off the onboard Led
digitalWrite(LED, HIGH); // LED LOW
Serial.println("Motion Detected! "); // print Motion Detected! on the serial monitor window
}
else
{
//else turn on the onboard LED
digitalWrite(LED, LOW); // LED High
Serial.println("Motion Ended!"); // print Motion Ended! on the serial monitor window
}
delay(500);
}
KEYPAD_WITHOUT_LIBRARY
#include<LiquidCrystal.h>

// Setting (RS,E,D4,D5,D6,D7) for particular pin number


LiquidCrystal lcd(17, 16, 22, 32, 33, 25, 26);

unsigned char Character1[8] = { 0x04, 0x1F, 0x11, 0x11, 0x1F, 0x1F, 0x1F, 0x1F }; /* Custom Character
1 */
unsigned char Character2[8] = { 0x01, 0x03, 0x07, 0x1F, 0x1F, 0x07, 0x03, 0x01 }; /* Custom Character
2 */
// Define variable c1,c2,c3,r1,r2,r3,r4 and del
int c1 = 13, c2 = 12, c3 = 14, c4 = 27 ;
int r1 = 1, r2 = 15, r3 = 2, r4 = 4;
int del = 600;

void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.createChar(0, Character1); /* Generate custom character */
lcd.createChar(1, Character2);
lcd.setCursor(0, 0);
lcd.print("Hello !");
delay(1000);
lcd.clear();
Serial.print("HELLO ");
Serial.println(" ");

/* use input pull-up feature of arduino, so that input c1 to c3 remains HIGH in absence input
signal
*/
pinMode(c1, INPUT_PULLUP);
pinMode(c2, INPUT_PULLUP);
pinMode(c3, INPUT_PULLUP);
pinMode(c4, INPUT_PULLUP);

// Set r1,r2,r3,r4 pins to OUTPUT mode


pinMode(r1, OUTPUT);
pinMode(r2, OUTPUT);
pinMode(r3, OUTPUT);
pinMode(r4, OUTPUT);
}

void loop() {
// Calling user defined function row1( ), row2( ), row3( ) and row4( ) next to each other
row1();
row2();
row3();
row4();
}

// Logic to function row1( )


void row1() {
/* This the same condition we understand in topic 9.2 i.e. one row should be Low and others should
be High to distinguish between other rows and to print row 1 data
*/
digitalWrite(r1, LOW);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(r4, HIGH);

/* We know high signal becomes low when it gets a direct path to ground */
/* Reading state of c1 and checking if it is LOW then print ‘1’ and if c2 is LOW print ‘2’ and if c3 is LOW
print ‘3’
*/
if (digitalRead(c1) == LOW) {
lcd.print("1"); delay(del);
Serial.print("1 ");
}
else if (digitalRead(c2) == LOW) {
lcd.print("2"); delay(del);
Serial.print("2 ");
}
else if (digitalRead(c3) == LOW) {
lcd.print("3"); delay(del);
Serial.print("3 ");
}
else if (digitalRead(c4) == LOW) {
lcd.print("A"); delay(del);
Serial.print("A");
}
//delay(del);
/*why do we not set single delay here instead three in if blocks? Because if we give delay in
if block then delay will executed only if any key has been pressed but if we give delay
here each time either key pressed or not delay will be executed which slows the
programming process.
*/
}
/* Similarly as we set logic for row1 we can set logic for other rows.
Keep row LOW which we want to set logic and others to be HIGH
But this time we will print ‘4’, ‘5’, ‘6’ on column key pressed instead of ‘1’, ‘2’, ‘3’.
*/
void row2() {
digitalWrite(r1, HIGH);
digitalWrite(r2, LOW);
digitalWrite(r3, HIGH);
digitalWrite(r4, HIGH);

if (digitalRead(c1) == LOW) {
lcd.print("4"); delay(del);
Serial.print("4 ");
}
else if (digitalRead(c2) == LOW) {
lcd.print("5"); delay(del);
Serial.print("5 ");
}
else if (digitalRead(c3) == LOW) {
lcd.print("6"); delay(del);
Serial.print("6 ");
}
else if (digitalRead(c4) == LOW) {
lcd.print("B"); delay(del);
Serial.print("B ");
}
}

/* In this we will print ‘7’, ‘8’, ‘9’ on column key pressed.


*/
void row3() {
digitalWrite(r1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, LOW);
digitalWrite(r4, HIGH);

if (digitalRead(c1) == LOW) {
lcd.print("7"); delay(del);
Serial.print("7 ");
}
else if (digitalRead(c2) == LOW) {
lcd.print("8"); delay(del);
Serial.print("8 ");
}
else if (digitalRead(c3) == LOW) {
lcd.print("9"); delay(del);
Serial.print("9 ");
}
else if (digitalRead(c4) == LOW) {
lcd.print("C"); delay(del);
Serial.print("C ");
}
}
/* In this we will print ‘*’, ‘0’, ‘#’ on column key pressed.
*/
void row4() {
digitalWrite(r1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(r4, LOW);

if (digitalRead(c1) == LOW) {
lcd.print("*"); delay(del);
Serial.print("* ");
}
else if (digitalRead(c2) == LOW) {
lcd.print("0"); delay(del);
Serial.print("0 ");
}
else if (digitalRead(c3) == LOW) {
lcd.print("#"); delay(del);
Serial.print("# ");
}
else if (digitalRead(c4) == LOW) {
lcd.print("D"); delay(del);
Serial.print("D ");
}
}

LCD_4 BIT AND 8 BIT


#include <LiquidCrystal.h>
/* Create object named lcd of the class LiquidCrystal */
//LiquidCrystal lcd(17, 16, 22, 1, 15, 2, 4, 13, 12, 14, 27); /* For 8-bit mode */
LiquidCrystal lcd(17, 16, 22, 19, 23, 18, 5); /* For 4-bit mode */

unsigned char Character1[8] = { 0x04, 0x1F, 0x11, 0x11, 0x1F, 0x1F, 0x1F, 0x1F }; /* Custom Character
1 */
unsigned char Character2[8] = { 0x01, 0x03, 0x07, 0x1F, 0x1F, 0x07, 0x03, 0x01 }; /* Custom Character
2 */

void setup() {
lcd.begin(16,2); /* Initialize 16x2 LCD */
lcd.clear(); /* Clear the LCD */
lcd.createChar(0, Character1); /* Generate custom character */
lcd.createChar(1, Character2);
}

void loop() {
lcd.setCursor(0,0); /* Set cursor to column 0 row 0 */
lcd.print("Welcome to"); /* Print data on display */
lcd.setCursor(0,1);
lcd.print("4 bit"); /* Write a character to display */
//lcd.write(1);
}
RELAY_CONTROL
int relay_1 = 16;
int relay_2 = 17;
int buzzer = 21;
//int redPin = 15;
//int greenPin = 14;
//int bluePin = 13;
void setup() {
// put your setup code here, to run once:
pinMode(relay_1, OUTPUT);
pinMode(relay_2, OUTPUT);
// pinMode(redPin, OUTPUT);
// pinMode(greenPin, OUTPUT);
// pinMode(bluePin, OUTPUT);
// digitalWrite(redPin, LOW);
// digitalWrite(greenPin, LOW);
// digitalWrite(bluePin, LOW);
pinMode(buzzer, OUTPUT);
digitalWrite(buzzer, HIGH);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(relay_1, HIGH);
delay(1000);
digitalWrite(relay_1, LOW);
delay(1000);
digitalWrite(relay_2, HIGH);
delay(1000);
digitalWrite(relay_2, LOW);
delay(1000);
digitalWrite(buzzer, LOW);
delay(1000);
digitalWrite(buzzer, HIGH);
delay(1000);
}

RGB
void setup() {
// initialize digital pin 13 as an output.
pinMode(22, OUTPUT);
pinMode(19, OUTPUT);
pinMode(23, OUTPUT);
}

// the loop function runs over and over again forever


void loop() {
while (1)
{
digitalWrite(22, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(22, LOW); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(19, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(19, LOW); // turn the LED on (HIGH is the voltage level)
delay(1000);
digitalWrite(23, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(23, LOW); // turn the LED on (HIGH is the voltage level)
delay(1000);

}
}

RS-485 RECEIVER
/*
* Company: Logsun Systems
* mail_ID: [email protected]
*
* Platform: ESP32-Dev Board
* Title: RS-485 Recevier Sample Code
* Description: The code performs following operations to test the onboard RS-485
- Configures the RS-485 hardware in recevicer mode.
- Wait for data to receive
- Put the RS-485 in transmit mode.
- Send the received data back to trasnmitter.
- To see the response,
Open the Serial Terminal,
Set the baud rate to 115200.
- Reset the board
- Send some data and wait for response.
- If you see the data you have entered returns back, RS-485 is working.

* Date: 14th October 2021


* author: Shreyas Deshpande.
*/

#define REDEPIN GPIO_NUM_5

/*
REDEPIN = 0 => Receive Data

REDEPIN = 1 => Transmit Data

*/

String Data = "";


char Buff[100] = {'\0'};
void setup()
{
pinMode(REDEPIN,OUTPUT);
Data.reserve(100);
digitalWrite(REDEPIN,LOW);

Serial.begin(9600);
}

void loop()
{

if(Serial.available()){
Data = Serial.readString();

Serial.flush();
delay(1000);
digitalWrite(REDEPIN,HIGH);
delay(100);
Data.toCharArray(Buff,Data.length()+1);
for(int i=0;i<Data.length();i++){
Serial.print(Buff[i]);
delay(100);
}
Serial.print("\n");
delay(120);

delay(100);
digitalWrite(REDEPIN,LOW);
delay(100);

}
}
RS-485 TRANSMITTER
/*
Company: Logsun Systems
mail_ID: [email protected]

Platform: ESP32-Dev Board


Title: RS-485 Trasnmitter Sample Code
Description: The code performs following operations to test the onboard RS-485
- Configures the RS-485 hardware in transmit mode.
- Transmit the count over RS-485
- Increment the count
- For more information please read the RS485 section of User Manual.

Date: 14th October 2021


author: Shreyas Deshpande.
*/

#define RXD2 16
#define TXD2 17
#define REDEPIN GPIO_NUM_22

/*
REDEPIN = 0 => Receive Data

REDEPIN = 1 => Transmit Data

*/

int count = 0;

void setup()
{
pinMode(REDEPIN, OUTPUT); //Define Direction Pin as OUTPUT
digitalWrite(REDEPIN, HIGH); // Put RS-485 in Transmit mode
Serial.begin(9600); //Initialize Serial Port
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
}

void loop()
{

Serial.println(count); //Send the count over RS-485


count++; //Increment the count
delay(1000);

if (count == 101) {
count = 0;
}
}
RTC-1307
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",


"Saturday" };

void setup() {
Serial.begin(115200);
Wire.begin(); //13 SDA 16 SCL

if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
// while (1) delay(10);
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
//rtc.adjust(DateTime(2025, 07, 26, 10, 16, 0));
}

// When time needs to be re-set on a previously configured device, the


// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}

void loop() {
DateTime now = rtc.now();

Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();

Serial.println();
delay(3000);
}

SD_TEST
/*
* Company: Logsun Systems
* mail_ID: [email protected]
*
* Platform: ESP32-Dev Board
* Title: SD_Card Sample Code
* Description: The code performs following operations to test the SD card connector.
- Create file on SD card with name "test.txt"
- Writes some data in file
- Append another data in file.
Open the Serial Terminal,
Set the baud rate to 115200.
- Reset the board
- Observe the process.

* Date: 14th October 2021


* author: Shreyas Deshpande.
*/

#include"microSD.h"

void setup()
{
Serial.begin(115200);
delay(200);

Serial.println("Initializing the SD card");


SPIClass spi = SPIClass(VSPI);
spi.begin(SCK,MISO,MOSI,CS);
delay(100);

if(!SD.begin(CS,spi,80000000)){
Serial.println("SD card Initialization Faild");
while(1);
}
else{
Serial.println("SD card Succesfully initialized");
}

uint8_t cardType = SD.cardType();

if(cardType == CARD_NONE){
Serial.println("No SD card attached");
return;
}

Serial.print("SD Card Type: ");


if(cardType == CARD_MMC){
Serial.println("MMC");
} else if(cardType == CARD_SD){
Serial.println("SDSC");
} else if(cardType == CARD_SDHC){
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}

uint64_t cardSize = SD.cardSize() / (1024 * 1024);


Serial.printf("SD Card Size: %lluMB\n", cardSize);
//Creating file on SD card

writeFile(SD,"/test.txt","Hello from Logsun, Test message\n\n");

delay(400);

appendFile(SD,"/test.txt","This is appended Message");

void loop()
{

void writeFile(fs::FS &fs, const char * path, const char * message){


Serial.printf("Writing file: %s\n", path);

File file = fs.open(path, FILE_WRITE);


if(!file){
Serial.println("Failed to open file for writing");
return;
}
if(file.print(message)){
Serial.println("File written");
} else {
Serial.println("Write failed");
}
file.close();

return;
}
void appendFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Appending to file: %s\n", path);

File file = fs.open(path, FILE_APPEND);


if(!file){
Serial.println("Failed to open file for appending");
return;
}
if(file.print(message)){
Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
file.close();

return;
}

SEVEN_SEGMENT_COUNTER
/* SevSeg Counter Example

Copyright 2020 Dean Reading

This example demonstrates a very simple use of the SevSeg library with a 4
digit display. It displays a counter that counts up, showing deci-seconds.
*/

#include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment controller object

void setup() {
byte numDigits = 4;
byte digitPins[] = {17, 16, 22, 21}; //Digits: 1,2,3,4 <--put one resistor (ex: 220 Ohms, or 330 Ohms,
etc, on each digit pin)
byte segmentPins[] = {1, 15, 2, 4, 13, 12, 14, 27}; //Segments: A,B,C,D,E,F,G,Period
bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
byte hardwareConfig = COMMON_ANODE; // See README.md for options
bool updateWithDelays = false; // Default 'false' is Recommended
bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
bool disableDecPoint = false; // Use 'true' if your decimal point doesn't exist or isn't connected

sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,


updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(90);
}

void loop() {
static unsigned long timer = millis();
static int deciSeconds = 0;

if (millis() - timer >= 100) {


timer += 100;
deciSeconds++; // 100 milliSeconds is equal to 1 deciSecond

if (deciSeconds == 10000) { // Reset to 0 after counting for 1000 seconds.


deciSeconds = 0;
}
sevseg.setNumber(deciSeconds, 1);
}

sevseg.refreshDisplay(); // Must run repeatedly


}

/// END ///

STEPPER_MOTOR_CONTROL
#include <Stepper.h>

const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution
const int stepsPerRevolution_0 = 0;
// ULN2003 Motor Driver Pins
#define IN1 19
#define IN2 23
#define IN3 18
#define IN4 5
int mSpeed = 0;
int dt = 500;
int Switch_1 = 36;
int Switch_2 = 39;
int Switch_3 = 34;
int Switch_4 = 35;
int Switch_5 = 32;

int dir1;
int dir2;

int SW1;
int SW2;
int SW3;
int SW4;
int SW5;
// initialize the stepper library
Stepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4);

void setup() {
// set the speed at 5 rpm

// initialize the serial port


Serial.begin(115200);
pinMode(Switch_1, INPUT);
pinMode(Switch_2, INPUT);
pinMode(Switch_3, INPUT);
pinMode(Switch_4, INPUT);
pinMode(Switch_5, INPUT);
digitalWrite(Switch_1, HIGH);
digitalWrite(Switch_2, HIGH);
digitalWrite(Switch_3, HIGH);
digitalWrite(Switch_4, HIGH);
digitalWrite(Switch_5, HIGH);
}

void loop() {
// step one revolution in one direction:
SW1 = digitalRead(Switch_1);
SW2 = digitalRead(Switch_2);
SW3 = digitalRead(Switch_3);
SW4 = digitalRead(Switch_4);
SW5 = digitalRead(Switch_5);
Serial.println(SW1);
Serial.println(SW2);
Serial.println(SW3);
Serial.println(SW4);
Serial.println(SW5);

Serial.print("dir1 = ");
Serial.println(dir1);
Serial.print("dir2 = ");
Serial.println(dir2);

if (SW5 == 0)
{
dir1 = 0;
dir2 = 0;
mSpeed = 0;
Stop();
}
if (SW1 == 0)
{
dir2 = 0;
dir1 = 1;
}
if (dir1 == 1)
{
myStepper.setSpeed(3);
Serial.println("clockwise");
myStepper.step(stepsPerRevolution / 10);
}
if (SW4 == 0)
{
dir1 = 0;
dir2 = 1;
}
if (dir2 == 1)
{
myStepper.setSpeed(3);
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution / 10);
}
if (SW3 == 0) {
dir1 = 0;
dir2 = 0;
Serial.println("Decrement");
mSpeed = mSpeed - 3;
delay(dt);
}
if (SW2 == 0) {
dir1 = 0;
dir2 = 0;
Serial.println("Increment");
mSpeed = mSpeed + 3;
delay(dt);
}
Serial.print("Motor Speed ");
Serial.println(mSpeed);

if (mSpeed > 18) {


dir1 = 0;
dir2 = 0;
mSpeed = 18;
}
if (mSpeed < -18) {
dir1 = 0;
dir2 = 0;
mSpeed = -18;
}
// if (mSpeed == 10) {
// mSpeed = 50;
// }
// if (mSpeed == -10) {
// mSpeed = -50;
// }
// if (mSpeed == 40 || mSpeed == 45) {
// mSpeed = 0;
// }
// if (mSpeed == -40 || mSpeed == -45) {
// mSpeed = 0;
// }
// if (mSpeed == 0) {
// myStepper.setSpeed(mSpeed);
// }
if (mSpeed > 0)
{
dir1 = 0;
dir2 = 0;
myStepper.setSpeed(mSpeed);
myStepper.step(stepsPerRevolution / 10);
}
if (mSpeed == -3)
{
dir1 = 0;
dir2 = 0;
myStepper.setSpeed(3);
myStepper.step(-stepsPerRevolution / 10);
}
if (mSpeed == -6)
{
dir1 = 0;
dir2 = 0;
myStepper.setSpeed(6);
myStepper.step(-stepsPerRevolution / 10);
}
if (mSpeed == -9)
{
dir1 = 0;
dir2 = 0;
myStepper.setSpeed(9);
myStepper.step(-stepsPerRevolution / 10);
}
if (mSpeed == -12)
{
dir1 = 0;
dir2 = 0;
myStepper.setSpeed(12);
myStepper.step(-stepsPerRevolution / 10);
}
if (mSpeed == -15)
{
dir1 = 0;
dir2 = 0;
myStepper.setSpeed(15);
myStepper.step(-stepsPerRevolution / 10);
}
if (mSpeed == -18)
{
dir1 = 0;
dir2 = 0;
myStepper.setSpeed(18);
myStepper.step(-stepsPerRevolution / 10);
}
}

void Stop()
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}

WIFISCAN
#include "WiFi.h"

void setup() {
Serial.begin(115200);

// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("Setup done");
}

void loop() {
Serial.println("scan start");

// WiFi.scanNetworks will return the number of networks found


int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
delay(10);
}
}
Serial.println("");

// Wait a bit before scanning again


delay(5000);
}

You might also like