AI-gestuurd obstakelvermijdingssysteem met Arduino en ultrasone sensoren
Onderdelen en benodigdheden
| SparkFun Arduino Pro Mini 328 - 5V/16MHz | × | 1 |
| Ultrasone Sensor HC-SR04 | × | 1 |
| SparkFun Dual H-Bridge Motor Driver L298 | × | 1 |
| DC Motor (60 RPM) | × | 2 |
| Wielen | × | 4 |
| Draadjes | × | 1 |
| Solderbare Breadboard Mini | × | 1 |
| 7.2V Li-Ion Accu | × | 1 |
| 9V Blokbatterij | × | 1 |
| 9V Batterijclip | × | 1 |
| PCBWay Custom PCB | × | 1 |
| C&K JS Series Schakelaar | × | 1 |
Benodigde tools en machines
- Soldeerbout
- Soldeerdraad (loodvrij)
- Soldeervloeistof
- Desolderpomp
- Kabelschraper/knipper
- Lijmpistool
- Dubbelzijdig tape
Apps en online diensten
- Arduino IDE
Over dit project
Obstakelvermijdende Robot met AI / Reinforcement Learning
Doel: De robot leert in "N" episodes de optimale actie (rechtsafslaan) om obstakels te vermijden.
Algoritme: Q-Learning (model-vrije reinforcement learning).
Hoe de L298N twee DC-motoren aanstuurt

Hoe de HC-SR04 afstand meet

De sensor stuurt een ultrasoon puls uit (Trig), meet de echo-tijd (Echo) en berekent afstand: afstand_cm = (duur_us / 2) / 29.1.
Belangrijke begrippen in Reinforcement Learning
- State (Toestand): De situatie van de robot. Hier 10 states (geïsoleerd voor complexiteit), eigenlijk 2: "veilig" (geen obstakel <15 cm) en "gevaar" (obstakel dichtbij).
- Action (Actie): Mogelijke acties in state "gevaar": 0=VOORUIT, 1=ACHTERUIT, 2=STOP, 3=LINKS. In state "veilig" alleen VOORUIT.
- Next State: Resultaat na actie: "GECRASHT" of "OVERLEEFD" (terug naar startstate).
- Q-Tabel / Q-Matrix: 10 states × 4 acties, initieel 0.0. Hogere waarde = betere actie in die state. Ongeldige acties = -1.0.
- Terminal State: Niet van toepassing (robot leert continu).
- Reward Matrix: Beloning per (state, actie). Voorbeeld: LINKS = +10, VOORUIT = -10, STOP = -1, ACHTERUIT = -2.
- Environment: De fysieke wereld waar de robot in opereert.
Hyperparameters
- Learning Rate (α = 0.1): Hoeveel nieuwe info oude info overschrijft. 0.1 = stabiel leren.
- Discount Factor (γ = 0.5): Belang van toekomstige beloningen. Lager = kortetermijn focus.
- Exploration Rate (ε = 0.90, decay 0.98/episode): Kans op willekeurige actie (exploratie) vs. beste bekende actie (exploitatie). ε daalt tot 0.5, dan reset naar 0.9 voor levenslange exploratie.
Q-Learning Algoritme
- Initialiseer Q(s,a) = 0.
- Observeer state s.
- Kies actie a via ε-greedy policy:
PROB = random(0,100)/100.0; if (PROB <= EPSILON) ACTION = random(0,4); // Explore else ACTION = argmax(Q[s]); // Exploit - Voer actie uit, ontvang reward r, observeer next state s'.
- Update Q-waarde (Bellman/Temporal Difference):
Q_OLD = Q[s][a]; Q_MAX = max(Q[s']); Q_NEW = (1-α)*Q_OLD + α*(r + γ*Q_MAX); Q[s][a] = Q_NEW; - Herhaal tot terminal state (hier: oneindig).

Code: Obstacle Avoidance Robot met Q-Learning
/* Obstacle Avoiding Robot using Q-LEARNING
Author: Varun Walimbe
Algorithm: Q-learning
Hardware: Arduino Pro Mini, HC-SR04, L298N, 2x 60RPM DC motors
*/
////////// HARDWARE PARAMETERS //////////
int TRIG_PIN = 7;
int ECHO_PIN = 8;
int duration;
float distance;
int M1 = 13, M2 = 12, M3 = 11, M4 = 10;
bool Obstacle = false;
int FLAG;
////////// Q-LEARNING PARAMETERS //////////
float ALPHA = 0.1;
float GAMMA = 0.5;
float EPSILON = 0.90;
int REWARD;
int EPISODES = 100;
int STATE = 0;
int ACTION = 0;
float PROB;
bool ACTION_TAKEN = false;
int NEXT_STATE;
const int STATES = 10;
int ACTIONS[4] = {1,2,3,4};
const int NUMBER_OF_ACTIONS = 4;
float Q[STATES][NUMBER_OF_ACTIONS] = {
{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},
{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}
};
int REWARDS[STATES][NUMBER_OF_ACTIONS] = {
{-10,-2,-1,10},{-10,-2,-1,10},{-10,-2,-1,10},{-10,-2,-1,10},{-10,-2,-1,10},
{-10,-2,-1,10},{-10,-2,-1,10},{-10,-2,-1,10},{-10,-2,-1,10},{-10,-2,-1,10}
};
float Q_OLD, Q_NEW, Q_MAX;
//////////////// ROBOT FUNCTIES ////////////////
void Forward() { digitalWrite(M1,LOW); digitalWrite(M2,HIGH); digitalWrite(M3,LOW); digitalWrite(M4,HIGH); }
void Backward() { digitalWrite(M1,HIGH); digitalWrite(M2,LOW); digitalWrite(M3,HIGH); digitalWrite(M4,LOW); }
void Left() { digitalWrite(M1,HIGH); digitalWrite(M2,LOW); digitalWrite(M3,LOW); digitalWrite(M4,HIGH); }
void Right() { digitalWrite(M1,LOW); digitalWrite(M2,HIGH); digitalWrite(M3,HIGH); digitalWrite(M4,LOW); }
void Stop() { digitalWrite(M1,LOW); digitalWrite(M2,LOW); digitalWrite(M3,LOW); digitalWrite(M4,LOW); }
bool Obstacle_Avoider() {
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = (duration/2)/29.1;
Obstacle = (distance < 15);
delay(10);
return Obstacle;
}
/////////// Q-LEARNING HULPFUNCTIES ///////////
float RANDOM(float ep) { return random(0,100)/100.0; }
float DECAY(float p) { return p * 0.98; }
int GET_STATE() { return random(0,10); }
float MAX(float Q_Table[][4], int next_s) {
float maxv = Q_Table[next_s][0];
for (int i=1;i<4;i++) if (Q_Table[next_s][i] > maxv) maxv = Q_Table[next_s][i];
return maxv;
}
int ARGMAX(float Q_Table[][4], int s) {
float maxv = Q_Table[s][0]; int maxi = 0;
for (int i=1;i<4;i++) if (Q_Table[s][i] > maxv) { maxv = Q_Table[s][i]; maxi = i; }
return maxi;
}
void Update(float Q_TABLE[][4], int s, int next_s, int a, int R, float lr, float df) {
Q_OLD = Q_TABLE[s][a];
Q_MAX = MAX(Q_TABLE, next_s);
Q_NEW = (1-lr)*Q_OLD + lr*(R + df*Q_MAX);
Serial.print("Q VALUE: "); Serial.println(Q_NEW);
Q_TABLE[s][a] = Q_NEW;
}
//////////////// MAIN LOOP //////////////////
void setup() {
Serial.begin(9600);
pinMode(TRIG_PIN,OUTPUT); pinMode(ECHO_PIN,INPUT);
pinMode(M1,OUTPUT); pinMode(M2,OUTPUT); pinMode(M3,OUTPUT); pinMode(M4,OUTPUT);
randomSeed(analogRead(A0));
STATE = 0;
Serial.println("TRAINING START OVER 5 SEC");
delay(5000);
}
void loop() {
//////// TRAINING ////////
for (int ep=0; ep= 10) NEXT_STATE = 0;
if (NEXT_STATE < 0) NEXT_STATE = 0;
Serial.print("STATE: "); Serial.println(STATE);
FLAG = 1; break;
}
}
// Kies actie (ε-greedy)
if (FLAG == 1) {
PROB = RANDOM(EPSILON);
if (PROB <= EPSILON) { ACTION = random(0,4); FLAG = 2; }
else { ACTION = ARGMAX(Q, STATE); FLAG = 2; }
}
// Voer actie uit
if (FLAG == 2) {
if (ACTION == 0) { Forward(); delay(1500); Stop(); REWARD = REWARDS[STATE][ACTION]; }
else if (ACTION == 1) { Backward(); delay(2500); Stop(); REWARD = REWARDS[STATE][ACTION]; }
else if (ACTION == 2) { Stop(); REWARD = REWARDS[STATE][ACTION]; }
else if (ACTION == 3) { Left(); delay(2000); Stop(); REWARD = REWARDS[STATE][ACTION]; }
ACTION_TAKEN = true; delay(500);
}
// Update Q-tabel
if (ACTION_TAKEN) {
Update(Q, STATE, NEXT_STATE, ACTION, REWARD, ALPHA, GAMMA);
STATE = NEXT_STATE;
EPSILON = DECAY(EPSILON);
if (EPSILON < 0.5) EPSILON = 0.9;
Serial.print("EPISODE ENDED: "); Serial.println(ep);
delay(7000);
}
}
//////// EVALUATIE (print Q-tabel) ////////
for (int y=0; y<=9; y++) {
Serial.println("SET OF Q VALUES WILL START:");
for (int l=0; l<=3; l++) {
Serial.print("Q VALUE: "); Serial.println(Q[y][l]);
delay(2000);
}
delay(2000);
}
Serial.println("EVALUATION ENDED");
//////// TESTING (deploy geleerde policy) ////////
while (true) {
Forward();
Obstacle = Obstacle_Avoider();
if (Obstacle) {
STATE = GET_STATE();
ACTION = ARGMAX(Q, STATE);
Serial.print("ACTION TAKEN: "); Serial.println(ACTION);
if (ACTION == 0) { Forward(); delay(1500); Stop(); }
else if (ACTION == 1) { Backward(); delay(1500); Stop(); }
else if (ACTION == 2) { Stop(); }
else if (ACTION == 3) { Left(); delay(2000); Stop(); }
}
}
}
Werken met de robot
De robot leert door herhaalde episodes. Aanvankelijk kiest hij willekeurig (exploratie), maar naarmate de Q-tabel vult, kiest hij vaker de actie met hoogste Q-waarde (exploitatie). De reward-matrix stimuleert LINKS (+10) en straft VOORUIT (-10) bij een obstakel. Na training kiest de robot consistent de geleerde actie.
Let op: De code gebruikt 10 states voor demonstratie van een complexere omgeving; in de praktijk volstaan 2 states. De ε-decay met reset zorgt voor continue exploratie, vergelijkbaar met menselijk leren.
Bestanden
- Code:
Obstacle Avoidance Robot using Q-LEARNING(C/C++) - Schema: Fritzing-bestand
obstacle_avoiding_robot_using_ai_DcoMCWIOFm.fzz
Productieproces
- Visie-gebaseerde Obstakel-Vermijdende Mobiele Robot op Raspberry Pi
- Bosch onthult AI-gedreven productieoplossing op Hannover Messe
- AI-gestuurde Robotica: High-tech Intelligente Machines
- AI Voorspelt Kwantumvoordeel: Neuraal Netwerk Voorspelt Systeemgedrag
- AI Is Meer Dan Een App – Het Is Een Krachtige Methodologie voor Zakelijk Succes
- Arrow Electronics lanceert AI Experience Tour met Intel, Microsoft, Nvidia
- Kunstmatige Intelligentie in Detailhandel: Bewezen Voordelen, Geen Hype
- Kubernetes Geeft AI-Startups Schaalbare, Gecontaineriseerde Oplossingen
- Hoe AI het Internet of Things-landschap transformeert
- Kunstmatige intelligentie is nog niet klaar voor industriële besturingssysteem-cyberbeveiliging
- AI-gestuurd Volgen van Amazon-ontbossing