Magic 3D Pen Documentation
The Magic 3D Pen is a innovative IoT component that allows users to create three-dimensional objects by extruding melted plastic filament through a heated nozzle. This pen can be controlled programmatically, enabling precise and complex 3D printing capabilities.
The Magic 3D Pen has the following pinout:
| Pin | Function |
| --- | --- |
| VCC | Power supply (5V) |
| GND | Ground |
| RX | Serial data input |
| TX | Serial data output |
| EN | Enable pin (active low) |
| STEP | Step motor control |
| DIR | Direction motor control |
The Magic 3D Pen communicates using a serial protocol at a baud rate of 9600. The device responds to the following commands:
| Command | Description |
| --- | --- |
| `G1 X<value> Y<value>` | Move the print head to the specified X and Y coordinates |
| `G1 F<value>` | Set the print speed to the specified value (in mm/s) |
| `M104 S<value>` | Set the extruder temperature to the specified value (in C) |
| `M106 S<value>` | Set the fan speed to the specified value (in %) |
Example 1: Basic 3D Printing using Arduino
This example demonstrates how to use the Magic 3D Pen with an Arduino board to print a simple square shape.
```cpp
#include <SoftwareSerial.h>
#define RX_PIN 2
#define TX_PIN 3
#define EN_PIN 4
SoftwareSerial magicPen(RX_PIN, TX_PIN);
void setup() {
magicPen.begin(9600);
pinMode(EN_PIN, OUTPUT);
digitalWrite(EN_PIN, LOW);
}
void loop() {
// Set extruder temperature to 200C
magicPen.println("M104 S200");
delay(5000);
// Move to starting position
magicPen.println("G1 X0 Y0");
delay(1000);
// Print a square shape
for (int i = 0; i < 4; i++) {
magicPen.println("G1 F50");
delay(1000);
magicPen.println("G1 X10");
delay(1000);
magicPen.println("G1 Y10");
delay(1000);
magicPen.println("G1 X0");
delay(1000);
}
// Disable extruder
magicPen.println("M104 S0");
delay(5000);
}
```
Example 2: Python Script for Advanced 3D Printing
This example demonstrates how to use the Magic 3D Pen with a Python script to print a complex 3D shape.
# Open the serial connection
ser = serial.Serial('/dev/ttyUSB0', 9600)
# Set extruder temperature to 200C
ser.write(b'M104 S200
')
ser.flush()
time.sleep(5)
# Load 3D model data from file
with open('model.gcode', 'r') as f:
lines = f.readlines()
# Send G-code commands to the Magic 3D Pen
for line in lines:
ser.write(line.encode() + b'
')
ser.flush()
time.sleep(0.1)
# Disable extruder
ser.write(b'M104 S0
')
ser.flush()
time.sleep(5)
# Close the serial connection
ser.close()
```
Example 3: Using the Magic 3D Pen with a Raspberry Pi
This example demonstrates how to use the Magic 3D Pen with a Raspberry Pi to create a web-based 3D printing interface.
```python
import serial
from flask import Flask, request
# Open the serial connection
ser = serial.Serial('/dev/ttyUSB0', 9600)
@app.route('/print', methods=['POST'])
def print_gcode():
gcode = request.form['gcode']
ser.write(gcode.encode() + b'
')
ser.flush()
return 'G-code sent to Magic 3D Pen!'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
```
These examples demonstrate the basic usage of the Magic 3D Pen and its capabilities. For more advanced usage, refer to the detailed documentation and technical specifications provided by the manufacturer.