This shows you the differences between two versions of the page.
|
pm:prj2026:jan.vaduva:silvia.berescu [2026/05/18 15:50] silvia.berescu [Pin Mapping — Arduino Labels vs ATmega328P Physical Pins] |
pm:prj2026:jan.vaduva:silvia.berescu [2026/05/23 22:38] (current) silvia.berescu [Hardware Progress] |
||
|---|---|---|---|
| Line 19: | Line 19: | ||
| The block diagram below shows all project modules and how they interact: | The block diagram below shows all project modules and how they interact: | ||
| - | {{ :pm:prj2026:jan.vaduva:silvia.berescu:bloc_diagram.png?600 |Block Diagram}} | + | {{ :pm:prj2026:jan.vaduva:bloc_diagram.png?600 |Block Diagram}} |
| Main modules and their roles: | Main modules and their roles: | ||
| Line 49: | Line 49: | ||
| ==== Pin Mapping — Arduino Labels vs ATmega328P Physical Pins ==== | ==== Pin Mapping — Arduino Labels vs ATmega328P Physical Pins ==== | ||
| - | The ATmega328P-XMINI board uses Arduino-style pin labels (D2, D3, A4 etc.) which correspond to the internal ATmega328P port/pin names. The table below clarifies this mapping for all pins used in this project: | + | The table below clarifies the mapping for all pins used in this project: |
| + | ^ ATmega Port/Pin ^ Used For ^ | ||
| + | | PD2 | HC-SR04 Front — TRIG | | ||
| + | | PD3 (INT1) | HC-SR04 Front — ECHO | | ||
| + | | PD4 | HC-SR04 Left — TRIG | | ||
| + | | PD5 **(OC0B)** | L298N ENB — PWM Motor Right | | ||
| + | | PD6 **(OC0A)** | L298N ENA — PWM Motor Left | | ||
| + | | PB0 | L298N IN1 | | ||
| + | | PB1 | L298N IN2 | | ||
| + | | PB2 | L298N IN3 | | ||
| + | | PB3 | L298N IN4 | | ||
| + | | PB4 | HC-SR04 Left — ECHO | | ||
| + | | PC4 (SDA) | LCD I2C — SDA | | ||
| + | | PC5 (SCL) | LCD I2C — SCL | | ||
| + | | VCC | 5V power input | | ||
| + | | GND | Ground | | ||
| - | ^ Arduino Label ^ ATmega Port/Pin ^ Physical Pin (DIP) ^ Used For ^ | + | **PD3/INT1:** connected to the INT1 external interrupt line, used for precise ECHO pulse measurement on the front sensor. Interrupt-driven measurement is more accurate than polling because the ATmega reacts instantly to the signal edge without busy-waiting. |
| - | | D2 | PD2 | 4 | HC-SR04 Front — TRIG | | + | |
| - | | D3 | PD3 (INT1) | 5 | HC-SR04 Front — ECHO | | + | |
| - | | D4 | PD4 | 6 | HC-SR04 Left — TRIG | | + | |
| - | | D5 | PD5 **(OC0B)** | 11 | L298N ENB — PWM Motor Right | | + | |
| - | | D6 | PD6 **(OC0A)** | 12 | L298N ENA — PWM Motor Left | | + | |
| - | | D8 | PB0 | 14 | L298N IN1 | | + | |
| - | | D9 | PB1 | 15 | L298N IN2 | | + | |
| - | | D10 | PB2 | 16 | L298N IN3 | | + | |
| - | | D11 | PB3 | 17 | L298N IN4 | | + | |
| - | | D12 | PB4 | 18 | HC-SR04 Left — ECHO | | + | |
| - | | A4 | PC4 (SDA) | 27 | LCD I2C — SDA | | + | |
| - | | A5 | PC5 (SCL) | 28 | LCD I2C — SCL | | + | |
| - | | VCC (pin 7) | VCC | 7 | 5V power input | | + | |
| - | | VCC (pin 20) | VCC | 20 | 5V power input | | + | |
| - | | GND (pin 8) | GND | 8 | Ground | | + | |
| - | | GND (pin 22) | GND | 22 | Ground | | + | |
| - | **D3 (PD3/INT1):** connected to the INT1 external interrupt line, used for precise ECHO pulse measurement on the front sensor. Interrupt-driven measurement is more accurate than polling because the ATmega reacts instantly to the signal edge without busy-waiting. | + | **PD5/OC0B and PD6/OC0A:** both are hardware PWM outputs of Timer0 (Channel B and Channel A respectively). Using both channels allows independent speed control of each motor, which is essential for straight-line driving. |
| - | **D5 (PD5/OC0B) and D6 (PD6/OC0A):** both are hardware PWM outputs of Timer0 (Channel B and Channel A respectively). Using both channels allows independent speed control of each motor, which is essential for straight-line driving — real DC motors are never perfectly identical and require small speed corrections. | + | **PB4:** plain GPIO pin used for HC-SR04 Left ECHO via polling. It has no special hardware function requirements since the left sensor uses software polling. |
| - | + | ||
| - | **D12 (PB4):** plain GPIO pin used for HC-SR04 Left ECHO via polling. D12 has no special hardware function requirements since the left sensor uses software polling. | + | |
| ==== Electrical Schematic ==== | ==== Electrical Schematic ==== | ||
| - | {{ :pm:prj2026:jan.vaduva:silvia.berescu:schematic.png?800 |Electrical Schematic}} | + | {{ :pm:prj2026:jan.vaduva:schematic_maze_solver.png?800 |Electrical Schematic}} |
| The schematic uses **net labels** instead of long wires for clarity. Two pins sharing the same net label name are electrically connected. | The schematic uses **net labels** instead of long wires for clarity. Two pins sharing the same net label name are electrically connected. | ||
| Line 91: | Line 88: | ||
| === Motor Control === | === Motor Control === | ||
| - | + | ||
| The L298N receives direction signals on IN1–IN4 from ATmega pins D8–D11: | The L298N receives direction signals on IN1–IN4 from ATmega pins D8–D11: | ||
| - | + | ||
| - | ^IN1^IN2^Motor Left behavior^ | + | ^ IN1 ^ IN2 ^ Motor Left behavior ^ |
| - | | HIGH | LOW | Forward | | + | | LOW | HIGH | Forward | |
| - | | LOW | HIGH | Reverse | | + | | HIGH | LOW | Reverse | |
| | LOW | LOW | Stop | | | LOW | LOW | Stop | | ||
| - | + | ||
| The same logic applies to IN3/IN4 for Motor Right. | The same logic applies to IN3/IN4 for Motor Right. | ||
| - | + | ||
| - | Speed is controlled via PWM on both **ENA** (D6/OC0A) and **ENB** (D5/OC0B) using Timer0 Fast PWM mode. Both jumper pairs (J2/J3 for ENA and J8/J9 for ENB) are removed from the L298N module to enable external PWM control. This allows independent speed adjustment for each motor to compensate for mechanical differences. | + | Speed is controlled via PWM on both **ENA** (D6/OC0A) and **ENB** |
| + | (D5/OC0B) using Timer0 Fast PWM mode. | ||
| === HC-SR04 Front Sensor === | === HC-SR04 Front Sensor === | ||
| Line 133: | Line 131: | ||
| ==== Hardware Progress ==== | ==== Hardware Progress ==== | ||
| + | {{ :pm:prj2026:jan.vaduva:hardware_progress.jpeg?600 |Assembled Robot}} | ||
| + | The image shows the fully wired robot with all components mounted and powered via the 2x18650 battery pack (7.4V). | ||
| + | |||
| + | === Component Identification === | ||
| + | |||
| + | * **ATmega328P-XMINI** — powered via the 5V regulated output of the L298N module. The onboard amber LED is lit, confirming the board is receiving stable 5V power from the battery pack through the L298N regulator. GND from the ATmega is connected to the breadboard ground rail to establish a common ground reference for all components. | ||
| + | * **L298N motor driver** — all 4 red indicator LEDs are on, confirming the module is powered and IN1–IN4 lines are in a defined logic state. The 5V regulated output feeds the breadboard power rail which distributes power to all logic components. | ||
| + | * **LCD 16x2 I2C** — the blue backlight is on, confirming the LCD module is receiving stable 5V power from the battery pack via the L298N regulated output. No characters are displayed yet as the firmware has not been uploaded at this stage. I2C communication will be verified once the ATmega is programmed. | ||
| + | * **HC-SR04 ultrasonic sensors** — both front and left sensors are mounted and connected to ATmega via the breadboard. | ||
| + | * **Breadboard** — distributes the 5V and GND rails from the L298N to all logic components. The red (+) rail carries 5V and the blue (-) rail carries GND, with the ATmega GND also connected to the same rail for common ground. | ||
| + | * **Battery holder 2x18650** — mounted on the chassis. | ||
| + | |||
| + | === What the Indicators Confirm === | ||
| + | |||
| + | * **L298N red LEDs on** — motor driver is powered and input pins IN1–IN4 are in a defined state, confirming correct power wiring. | ||
| + | * **LCD backlight on** — the 5V regulated output of the L298N is confirmed working, supplying stable power to the LCD module via the breadboard power rail. | ||
| + | * **ATmega amber LED on** — microcontroller is powered and operational. | ||
| + | * **Common GND established** — ATmega GND pin is connected to the breadboard ground rail shared with L298N, sensors and LCD, ensuring all components share the same voltage reference. | ||
| ===== Software Design ===== | ===== Software Design ===== | ||
| - | ==== Lab Concepts Used ==== | + | ==== Development Environment ==== |
| - | ^ Lab concept ^ Usage in project ^ | + | * **Visual Studio Code + PlatformIO Core 6.1.19** |
| - | | **GPIO** | TRIG/ECHO sensor pins, motor direction pins IN1–IN4 | | + | * **Framework:** Arduino (avr-minicore) used only as build system, all peripheral code written directly on AVR registers |
| - | | **PWM** | Timer0 Fast PWM on OC0A (D6) and OC0B (D5) — both motors | | + | * **Compiler:** avr-gcc (toolchain-atmelavr 7.3.0) |
| - | | **I2C** | TWI communication with LCD 16x2 via PCF8574 on A4/A5 | | + | * **Upload:** custom avrdude with ''xplainedmini'' programmer over USB mEDBG |
| - | | **UART** | Debug data transmission at 9600 baud via TX (D1) | | + | |
| - | | **Interrupts** | INT1 on D3 (PD3) for precise front ECHO pulse measurement | | + | ==== Project Structure ==== |
| + | |||
| + | ^ File ^ Role ^ | ||
| + | | ''main.cpp'' | Motor functions, navigation algorithm, setup/loop | | ||
| + | | ''sensors.cpp/.h'' | HC-SR04 front (INT1 interrupt) and left (polling) | | ||
| + | | ''lcd.cpp/.h'' | TWI/I2C driver + HD44780 LCD functions | | ||
| + | |||
| + | ==== Lab Concepts Used and Justification ==== | ||
| + | |||
| + | ^ Lab concept ^ Justification ^ | ||
| + | | **GPIO** | TRIG/ECHO sensor pins and motor direction pins controlled directly via DDRx/PORTx registers | | ||
| + | | **PWM** | Timer0 Fast PWM on OC0A and OC0B for independent speed control of each motor; allows straight-line correction by setting different OCR values | | ||
| + | | **Interrupts** | INT1 on PD3 for front HC-SR04 ECHO, interrupt-driven measurement is more accurate than polling because the MCU reacts instantly to signal edges without busy-waiting | | ||
| + | | **I2C** | LCD 16x2 via PCF8574 on PC4/PC5; TWI implemented on registers following the same pattern as the lab | | ||
| + | | **Timers** | Timer0 for PWM generation; Timer1 as free-running counter inside INT1 ISR to timestamp ECHO pulse edges and compute distance | | ||
| ==== Navigation Algorithm ==== | ==== Navigation Algorithm ==== | ||
| - | The robot uses the **Left-Hand Wall Follower** algorithm: | + | The robot implements the Left-Hand Wall Follower algorithm. The maze is constructed so the robot always has a left wall present. The decision logic runs continuously in the main loop: |
| + | |||
| + | - If **left is free** (distance > 20cm): turn left 90° then move forward — this is the core left-hand rule, taking every available left turn | ||
| + | - Else if **front is free** (distance > 20cm): move forward, follow the left wall | ||
| + | - Else: turn right 90°, blocked on both sides, only option is right | ||
| + | |||
| + | This guarantees finding the exit in any simply connected maze. | ||
| + | |||
| + | ==== Sensor Implementation ==== | ||
| + | |||
| + | The **front sensor** uses INT1 hardware interrupt for precise ECHO pulse measurement. Timer1 is started on the rising edge of ECHO and stopped on the falling edge. Distance is computed from the elapsed ticks. This approach is more accurate than polling and frees the CPU between measurements. | ||
| + | |||
| + | The **left sensor** uses GPIO polling. PB4 (D12) was chosen instead of D5 because D5 (OC0B) was needed for ENB PWM. Polling is acceptable here since the left wall changes slowly, precision requirements are lower than for the front sensor. | ||
| + | |||
| + | ==== Motor Control ==== | ||
| + | |||
| + | Timer0 Fast PWM at 7.8kHz drives both motors independently via OC0A (left) and OC0B (right). | ||
| + | |||
| + | **Important finding:** The motor direction logic is inverted, determined experimentally during hardware testing. | ||
| + | |||
| + | Motor B runs at a slightly higher PWM value than Motor A to compensate for mechanical differences between the two motors, ensuring straight-line driving. | ||
| + | |||
| + | ==== Calibration ==== | ||
| + | |||
| + | * **Turn delay:** calibrated by placing the robot on a flat surface, calling the turn function, and measuring the resulting angle physically. Adjusted until 90° was achieved consistently. | ||
| + | * **Motor speeds:** OCR0A=180-200, OCR0B=190-200 — calibrated by observing straight-line driving. Motor B speed increased until drift was eliminated. | ||
| + | * **Distance thresholds:** DIST_FRONT_STOP and DIST_LEFT_FAR both set to 20cm — chosen to match the corridor width of the test maze and provide sufficient stopping margin. | ||
| + | |||
| + | |||
| + | ==== Validation ==== | ||
| + | |||
| + | Each component was validated independently before integration: motors tested individually for direction and speed, each sensor tested by placing hand at known distances and verifying LCD readings, turn angles verified physically on grid paper. Full algorithm validated in test maze sections observing behavior for each decision case. | ||
| - | - If left is **free** (distance > 20cm): turn left 90° + move forward | + | ==== Demo Video ==== |
| - | - Else if front is **free** (distance > 20cm): move forward | + | |
| - | - Else: turn right 90° | + | |
| + | //To be added after final maze test// | ||
| ===== Results ===== | ===== Results ===== | ||
| Line 170: | Line 230: | ||
| | Week 2 | Chassis assembly, mounting motors and wheels | Done | | | Week 2 | Chassis assembly, mounting motors and wheels | Done | | ||
| | Week 3 | L298N wiring + electrical schematic | Done | | | Week 3 | L298N wiring + electrical schematic | Done | | ||
| - | | Week 4 | HC-SR04 integration + distance measurement test | Planned | | + | | Week 4 | HC-SR04 integration + LCD I2C integration | Done | |
| - | | Week 5 | LCD I2C integration + data display | Planned | | + | | Week 5 | Navigation algorithm implementation + maze test | Planned | |
| - | | Week 6 | Navigation algorithm implementation + maze test | Planned | | + | | Week 6 | Code optimization, final demo | Planned | |
| - | | Week 7 | 90° turn calibration, code optimization, final demo | Planned | | + | |
| ===== Bibliography / Resources ===== | ===== Bibliography / Resources ===== | ||