בקר ארדואינו

✦ בנוי על ידי פהד גאנם ✦

Uno R3: ATmega328P  ·  5 V  ·  32 KB  ·  14 DIO + 6 AIN  ·  20 mA/pin   |   PLC: 24 V  ·  opto-isolated I/O  ·  relay 2 A
0
0

Verify → compile only   |   Upload → compile + send   |   Tools > Board  ·  Tools > Port   |   CH340 / CP2102 driver
0
void setup() {
  // רץ פעם אחת, מיד אחרי ההפעלה או אחרי לחיצה על אתחול
}

void loop() {
  // רץ שוב ושוב, בלי סוף, כל עוד יש מתח
}

R = VCC − VfI     Imax = 20 mA / pin   ·   200 mA total   |   PR = I2 · R
220 Ω
0 mA
0 V
0 V
0 mW
// One LED on pin 8, through a series resistor to ground.
// R = (Vcc - Vf) / I  ->  (5 - 2) / 0.014 = 214 ohm  ->  use 220
const int LED = 8;

void setup() {
  pinMode(LED, OUTPUT);
}

void loop() {
  digitalWrite(LED, HIGH);
  delay(700);
  digitalWrite(LED, LOW);
  delay(300);
}

VR = VCC − Vf = 5 − 3.2 = 1.8 V
R = 1.80.012 = 150 Ω
P = I2 · R = 0.0122 · 150 = 0.0216 W ≈ 22 mW

INPUT_PULLUP → Rint ≈ 20–50 kΩ   |   pressed → LOW   |   tbounce ≈ 1–10 ms   |   tdebounce ≥ 20 ms
0.30×
HIGH
0
0
// The naive version. No pull-up, no debounce.
const int BTN = 2;
const int LED = 8;

int count = 0;
int last  = HIGH;

void setup() {
  pinMode(BTN, INPUT);
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int raw = digitalRead(BTN);
  if (raw == LOW && last == HIGH) {
    count = count + 1;
    Serial.println(count);
  }
  last = raw;
  digitalWrite(LED, raw == LOW ? HIGH : LOW);
}
// Internal pull-up: the pin idles HIGH, a press pulls it LOW.
// A change is accepted only after it has held for 25 ms.
const int BTN = 2;
const int LED = 8;

int count = 0;
int lastRead = HIGH;
int stable   = HIGH;
unsigned long lastEdge = 0;

void setup() {
  pinMode(BTN, INPUT_PULLUP);
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int raw = digitalRead(BTN);

  if (raw != lastRead) {
    lastRead = raw;
    lastEdge = millis();
  }

  if (millis() - lastEdge > 25 && stable != lastRead) {
    stable = lastRead;
    if (stable == LOW) {
      count = count + 1;
      Serial.println(count);
    }
  }

  digitalWrite(LED, stable == LOW ? HIGH : LOW);
}

I = 510000 = 0.5 mA
P = V2R = 2510000 = 2.5 mW
n = 60.1 = 60

D = tONT · 100%     Vavg = D · VCC     analogWrite(pin, 0…255)   |   Uno: ~490 Hz  ·  pins 3 5 6 9 10 11
127
50 %
2.49 V
1.02 ms
2.04 ms
// PWM fade on pin 9.
// analogWrite takes 0..255 - that is a duty cycle, not a voltage.
const int LED = 9;

void setup() {
  pinMode(LED, OUTPUT);
}

void loop() {
  for (int d = 0; d <= 255; d = d + 5) {   // brighter
    analogWrite(LED, d);
    delay(20);
  }
  for (int d = 255; d >= 0; d = d - 5) {   // dimmer
    analogWrite(LED, d);
    delay(20);
  }
}
00 %0.00 V
6425 %1.25 V
12750 %2.49 V
19175 %3.75 V
255100 %5.00 V

D = 51255 · 100 = 20 %
T = 1490 = 2.04 ms
tON = 0.2 · 2.04 = 0.41 ms
Vavg = 0.2 · 5 = 1.0 V
n ≈ 0.2 · 3000 = 600 rpm

raw = VinVref · 1023     ΔV = 51023 = 4.89 mV   |   Vout = VCC · R2R1 + R2
50 %
10.0 kΩ
2.50 V
512
128
4.89 mV
// A0 reads a voltage divider. The LED on pin 9 follows it.
const int POT = A0;
const int LED = 9;

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int raw = analogRead(POT);              // 0 .. 1023, never volts
  float volts = raw * 5.0 / 1023.0;       // this is the conversion
  int duty = map(raw, 0, 1023, 0, 255);   // 10 bits -> 8 bits

  analogWrite(LED, duty);

  Serial.print("raw=");
  Serial.print(raw);
  Serial.print("  V=");
  Serial.print(volts, 2);
  Serial.print("  duty=");
  Serial.println(duty);

  delay(300);
}

VA0 = 5 · RfixRLDR + Rfix
V = 5 · 10210 = 0.238 V  →  raw = 0.2385 · 1023 = 49
V = 5 · 1011 = 4.55 V  →  raw = 4.555 · 1023 = 930
ΔV = 51023 = 4.89 mV

frame = 1 start + 8 data + 1 stop = 10 bits   |   tbit = 1baud   |   chars/s = baud10
9600
104 µs
960
LOW
// The board listens to the computer over the same USB cable.
// Type ON, OFF or BLINK in the monitor and press send.
const int LED = 8;
String cmd = "";

void handle(String s) {
  if (s == "ON") {
    digitalWrite(LED, HIGH);
    Serial.println("led is on");
  } else if (s == "OFF") {
    digitalWrite(LED, LOW);
    Serial.println("led is off");
  } else if (s == "BLINK") {
    for (int i = 0; i < 3; i = i + 1) {
      digitalWrite(LED, HIGH);
      delay(200);
      digitalWrite(LED, LOW);
      delay(200);
    }
    Serial.println("blinked 3 times");
  } else {
    Serial.print("unknown command: ");
    Serial.println(s);
  }
}

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  Serial.println("ready. commands: ON OFF BLINK");
}

void loop() {
  while (Serial.available() > 0) {
    char c = Serial.read();
    if (c == '\n' || c == '\r') {      // a whole line arrived
      if (cmd.length() > 0) {
        handle(cmd);
        cmd = "";
      }
    } else {
      cmd = cmd + c;                   // one character at a time
    }
  }
}

n = 24 · 10 = 240 bit
t = 2409600 = 25 ms
nloop = 100025 = 40
t = 240115200 = 2.1 ms  →  480

IB = Vpin − VBERB     IC = β · IB     vL = L · didt   |   Icoil ≈ 70–100 mA
0 mA
0 mA
0 V
// A ready-made relay module on pin 7 switches the load.
// The pin drives the module input only - never a coil directly.
const int RELAY = 7;

void setup() {
  pinMode(RELAY, OUTPUT);
  digitalWrite(RELAY, LOW);      // start with the load off
}

void loop() {
  digitalWrite(RELAY, HIGH);     // contact closes, load is fed
  delay(3000);
  digitalWrite(RELAY, LOW);      // contact opens
  delay(3000);
}
80 mA
4.3 mA≈ 300 V
4.3 mA≈ 5.7 V
3.5 mA≈ 5.7 V

IC = 562 = 80.6 mA
IB = 3 · 80.6100 = 2.42 mA
RB = 5 − 0.70.00242 = 1777 Ω  →  1.5 kΩ
Ipin = 4.31500 = 2.87 mA < 20 mA

Tframe = 20 ms  →  f = 50 Hz   |   tpulse : 1 ms → 0°  ·  1.5 ms → 90°  ·  2 ms → 180°
90°
90 °
1.47 ms
60 mA
5.0 V
#include <Servo.h>

// The library builds the 50 Hz pulse train in the background.
// We only write an angle in degrees.
Servo arm;

void setup() {
  arm.attach(9);        // signal wire on pin 9
  arm.write(90);        // start in the middle
  delay(500);
}

void loop() {
  for (int a = 0; a <= 180; a = a + 2) {
    arm.write(a);
    delay(15);          // give the arm time to get there
  }
  delay(400);
  for (int a = 180; a >= 0; a = a - 2) {
    arm.write(a);
    delay(15);
  }
  delay(400);
}

θ = 1.75 − 12 − 1 · 180 = 135°
f = 10.02 = 50 Hz
D = 1.7520 · 100 = 8.75 %

TX → RX  ·  RX ← TX   |   SoftwareSerial(10, 11)   |   9600 baud
9600 bps
0
0
#include <SoftwareSerial.h>

// HC-05 module TX  -> pin 10 (our RX)
// HC-05 module RX  <- pin 11 (our TX)
// Pins 0 and 1 stay free for the USB monitor.
SoftwareSerial BT(10, 11);

const int LED = 9;
String cmd = "";

void apply(String s) {
  if (s == "ON") {
    analogWrite(LED, 255);
    BT.println("on");
  } else if (s == "DIM") {
    analogWrite(LED, 60);
    BT.println("dim");
  } else if (s == "OFF") {
    analogWrite(LED, 0);
    BT.println("off");
  } else {
    BT.print("unknown: ");
    BT.println(s);
  }
  Serial.print("from phone: ");
  Serial.println(s);
}

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(9600);       // the cable, for debugging
  BT.begin(9600);           // the module, for the phone
  Serial.println("bridge ready");
}

void loop() {
  while (BT.available() > 0) {
    char c = BT.read();
    if (c == '\n' || c == '\r') {
      if (cmd.length() > 0) {
        apply(cmd);
        cmd = "";
      }
    } else {
      cmd = cmd + c;
    }
  }
}
VCC5V
GNDGND
TXDD10
RXDD11

tbit = 19600 = 104.2 µs  →  tchar = 10 · 104.2 = 1.042 ms
"DIM\n" = 4 · 10 = 40 bit  →  t = 40 · 104.2 µs = 4.17 ms
n = 14.17 ms = 240
ttotal = 4.17 + 4.17 = 8.33 ms
tbit = 1115200 = 8.68 µs

TON > TOFF   ·   ΔT = TON − TOFF   |   if (millis() − last ≥ period)   |   LM35: θ = V0.01
28.0 °C
3.0 °C
28.0 °C
30.0 °C
27.0 °C
0
#include <SoftwareSerial.h>

// Room thermostat: LM35 on A0, fan through an isolated relay module on D7,
// status LED on D8, and a report to the cable and to the phone once a second.
SoftwareSerial BT(10, 11);

const int SENSOR = A0;
const int RELAY  = 7;
const int LED    = 8;

// Two thresholds, never one. The gap between them is the hysteresis.
const float ON_C  = 30.0;
const float OFF_C = 27.0;

bool fan = false;
unsigned long lastReport = 0;

float readCelsius() {
  int raw = analogRead(SENSOR);
  float volts = raw * 5.0 / 1023.0;
  return volts * 100.0;              // LM35: 10 mV per degree
}

void setup() {
  pinMode(RELAY, OUTPUT);
  pinMode(LED, OUTPUT);
  digitalWrite(RELAY, LOW);
  Serial.begin(9600);
  BT.begin(9600);
  Serial.println("thermostat ready");
}

void loop() {
  float t = readCelsius();

  if (fan == false && t >= ON_C)  fan = true;
  if (fan == true  && t <= OFF_C) fan = false;

  digitalWrite(RELAY, fan ? HIGH : LOW);
  digitalWrite(LED,   fan ? HIGH : LOW);

  // No delay here. The loop stays free to read the sensor and the module.
  if (millis() - lastReport >= 1000) {
    lastReport = millis();
    Serial.print("T=");
    Serial.print(t, 1);
    Serial.print("  fan=");
    Serial.println(fan ? 1 : 0);
    BT.print("T=");
    BT.println(t, 1);
  }
}

V = 30 · 0.01 = 0.30 V
raw = 0.305 · 1023 = 61
Δraw = 0.015 · 1023 = 2.05
Δθ = 22.05 = 0.98 °C
🏠