Evan Rust
Published © GPL3+

3D Room Visualizer

With only an arduino board, two servos, and an hc-sr04, and some python, you can create a room scanner that is capable of showing depth!

BeginnerShowcase (no instructions)2 hours34,845
3D Room Visualizer

Things used in this project

Hardware components

Servos (Tower Pro MG996R)
×2
HC-SR04 Ultrasonic Distance Sensor
×1
Male/Male Jumper Wires
×1
Arduino Nano R3
Arduino Nano R3
×1

Software apps and online services

Arduino IDE
Arduino IDE
python IDE

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Schematics

3d Imager Schematic

Attach as seen: servo ground wires to GND, power cables to 5V, and data wires to D9 & D10.

Code

Python Code

Python
Just run as-is. You may need to install pySerial and PILLOW
import PIL.Image
import serial
import time
img = PIL.Image.new('RGB', (100,100), "red") # create a new black image
pixels = img.load() # create the pixel map
ser = serial.Serial('COM1',9600)
for i in range(100):
    for j in range(100):
        msg = ser.readline()
        value = int(msg)
        print value
        time.sleep(.2)
        pixels[i,j] = (value, 0, 100)
img.show()

Arduino Code

C/C++
Upload as-is. Change pins if needed.
#include <Servo.h>
Servo servox; Servo servoy;
int servoPosx = 130;
int servoPosy = 130;
int pingPin = 3;
int echoPin = 4;
long duration;
long ping()
{
  // Send out PING))) signal pulse
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);
  
  //Get duration it takes to receive echo
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  
  //Convert duration into distance
  return duration;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
servox.attach(9);
servox.write(130);
servoy.attach(10);
servoy.write(30);
}

void loop() {
  
  // put your main code here, to run repeatedly:
  for(int i=0;i<101;i++){
for(int x=0;x<101;x++){
    int distance = microsecondsToCentimeters(ping());
    Serial.println(distance);
    if (servoPosx <30){
      servoPosx = 130;
    }
    servoPosx -= 1;
    servox.write(servoPosx);
    delay(200);
  }
if (servoPosy > 130){
  servoPosy = 30;
}
servoPosy +=1;
servoy.write(servoPosy);
delay(200);
} 
}

Credits

Evan Rust

Evan Rust

122 projects • 1087 followers
IoT, web, and embedded systems enthusiast. Contact me for product reviews or custom project requests.

Comments