November 11, 2025

Azure Digital Twins Tutorial: Building Digital Twin Solutions from Scratch

Blog Details Image

Azure Digital Twins provides a platform for creating virtual representations of physical objects and systems. This comprehensive tutorial demonstrates building a complete digital twin solution, from defining models using the Digital Twins Definition Language (DTDL) to updating twins with real-time sensor data. We'll create a digital twin for a Raspberry Pi measuring temperature and humidity, connecting physical sensor readings to the digital representation.

Understanding Azure Digital Twins enables developers and architects to build IoT solutions that mirror real-world environments in the cloud.

What is Azure Digital Twins

Azure Digital Twins is a platform that enables you to create and interact with real-world entities in the digital space. More generally, a digital twin is a virtual representation of a physical object or system—a pump, car, building, factory, or production process.

The main aim of a digital twin is creating a digital copy of a physical object that receives inputs from sensors gathering data from that object. Through continuous streams of real-time data, the digital copy stays synchronized with its physical counterpart, making it possible to apply technologies like machine learning to produce predictions and simulations.

Creating Digital Twins: The Process

Consider a compressor on a factory floor as our physical object. The compressor has instruments measuring motor temperature, vibration, and motor current.

To create a digital twin:

  1. Model the Object: Define what the compressor looks like in terms of structural composition, technical properties, and measured physical parameters (temperature, current, vibration)
  2. Store the Model: Save the model definition in digital format
  3. Create the Twin: Use the model to instantiate a digital twin of the compressor
  4. Connect to Physical Object: Establish communication mechanisms between the digital twin and real-world compressor
  5. Continuous Updates: Continuously update the twin with data from the physical compressor
  6. Replicate for Similar Objects: Use the same model to create additional instances for other compressors of the same type, connecting each instance to its physical counterpart

Digital Twins Definition Language (DTDL)

Azure Digital Twins uses the Digital Twins Definition Language (DTDL) for creating models. While Microsoft created DTDL for the Azure platform, they've since brought it to organizations like the Digital Twin Consortium.

DTDL uses JSON-LD, an open language similar to JSON, for expressing models in a structured, machine-readable format.

Four Key Elements of DTDL Models

A digital twin model in DTDL is composed of four key elements:

Properties

Properties represent the state of the physical entity. Using the compressor example, properties could include:

  • Name
  • Power rating
  • Serial number
  • Color

Properties require a storage mechanism that retains data permanently and allows updates as needed.

Telemetry

Telemetry is an event stream representing measurements of physical parameters—typically sensor readings like motor temperature, vibration, and motor current.

Unlike properties, telemetry is not stored on the digital twin. It is processed as it arrives and sent to external systems if required for analytics or archiving.

Components

Components allow including separate models as part of your model. For example, a motor could be defined as a standalone model and then used as a component of a compressor model through referencing.

This component-based approach promotes reusability and modular design.

Relationships

Relationships define how your digital twin is related to other digital twins. Examples:

  • A factory floor digital twin contains a production line digital twin
  • An air compressor supplies pressure to a control unit

Connecting digital twins via relationships creates a twin graph (sometimes called a knowledge graph) representing physical entities in your entire environment while reflecting their interactions.

Interface Structure

All elements of a digital twin model are enclosed in an interface, which serves as the container for the model definition.

Creating a Digital Twin Model with DTDL

Let's create a digital twin model for a factory with temperature and humidity measurements from a Raspberry Pi sensor.

Digital twin models can be written in any text editor and saved with a .json extension. Here's the structure:

json

{
 "@id": "dtmi:demo:factory;1",
 "@type": "Interface",
 "@context": "dtmi:dtdl:context;2",
 "displayName": "Factory",
 "contents": [
   {
     "@type": "Property",
     "name": "temperature",
     "schema": "double"
   },
   {
     "@type": "Property",
     "name": "humidity",
     "schema": "double"
   }
 ]
}

Required Items

@id: The digital twin model identifier (dtmi:demo:factory;1)

@type: Must be set to "Interface" to indicate this is a model definition

@context: Used to process the interface, must always be set to the DTDL context version

displayName: User-friendly name for displaying the model

Contents Array

Inside the contents array, define all items of the digital twin model:

Temperature Property: Type "Property" with "double" schema. We use Property type because we want this value stored on the digital twin for later retrieval. Telemetry type doesn't get stored—it's processed on arrival.

Humidity Property: Also type "Property" with "double" schema.

Schema Types

The schema can be primitive types (integer, double, string, boolean) or complex types (object, enum, map).

Advanced Features

Models can include relationships and components:

json

{
 "@type": "Relationship",
 "name": "contains",
 "target": "dtmi:demo:productionLine;1"
}

json

{
 "@type": "Component",
 "name": "compressor",
 "schema": "dtmi:demo:compressor;1"
}

Custom Models vs Ontologies

This example creates a custom model. Alternatively, use ontologies—pre-built models for specific industries. Ontologies represent ongoing efforts to standardize models. The Digital Twins Definition Language ontology for smart cities demonstrates this approach.

Setting Up Azure Digital Twins Instance

After creating your model, upload it to an Azure Digital Twins instance on Azure Cloud to make it available for creating twins in the digital space.

Creating the Instance

  1. Log into the Azure portal
  2. Select "Create a resource"
  3. Search for "Azure Digital Twins"
  4. Click Create
  5. Select your subscription
  6. Create or select a resource group
  7. Choose the location (region)
  8. Provide an instance name (for example, "my_demo_adt_instance")
  9. Review and create

The deployment takes a few minutes. Once complete, note the instance name, hostname, and resource group—you'll need these for connecting client applications.

Configuring Access Permissions

If using a Microsoft account, set up user access permissions for a corporate account to enable client application access:

  1. Go to Access Control (IAM)
  2. Click "Add" and "Add role assignment"
  3. Select "Azure Digital Twins Data Owner"
  4. Add your corporate account
  5. Save

This grants permissions to manage the instance, upload models, create twins, and execute queries.

Using Azure Digital Twins Explorer

Azure Digital Twins provides APIs and SDKs for C#, Java, Python, JavaScript, and Go. To interact with your instance—uploading models, creating twins, querying twins, creating relationships—you'd typically develop client applications using these APIs and SDKs.

However, Microsoft developed Azure Digital Twins Explorer, a Node.js client application with a graphical interface for interacting with Azure Digital Twins instances.

Azure Digital Twins Explorer enables:

  • Uploading and exploring models
  • Creating and editing twin graphs
  • Running queries against twin graphs
  • Visualizing twin graphs

Installing Azure Digital Twins Explorer

  1. Ensure Node.js version 10 or later is installed on your machine
  2. Download the sample code from the GitHub repository
  3. Extract the downloaded ZIP file
  4. Sign into your Azure account using your machine (the app uses host machine Azure credentials)

Running the Explorer

  1. Open a command prompt or terminal
  2. Navigate to the client/src directory in the extracted folder
  3. Run npm install to install dependencies
  4. Run npm run start to launch the application
  5. Enter your Azure Digital Twins instance hostname when prompted

The Explorer opens in your web browser, providing a visual interface for managing your digital twins.

Uploading Models

In Azure Digital Twins Explorer:

  1. Click the "Upload a model" icon
  2. Select your model JSON file
  3. The model appears in the models panel
  4. Expand the model to see its properties and telemetry

Creating Digital Twins

  1. Select the model you want to instantiate
  2. Click "Create a Twin"
  3. Provide a twin ID (for example, "factory_01_2021")
  4. The twin appears in the twin graph visualization

Querying Twins

Use the query panel to retrieve twins:

sql

SELECT * FROM digitaltwins

This returns all twins in your instance. You can write more complex queries filtering by properties, relationships, or other criteria.

Connecting IoT Hub and Azure Functions

To update digital twins with real sensor data, create a data pipeline:

  1. IoT Device: Sends sensor data to IoT Hub
  2. IoT Hub: Receives device messages
  3. Azure Function: Triggered by IoT Hub events, updates digital twin properties
  4. Azure Digital Twins: Stores updated twin state

Creating an IoT Hub

  1. Create a new IoT Hub resource in Azure
  2. Register your device (use the same ID as your digital twin—for example, "factory_01_2021")
  3. Copy the device connection string for your IoT device code

Creating an Azure Function

  1. Create a Function App in Azure
  2. Create a new function with an Event Hub trigger
  3. Connect the trigger to your IoT Hub's built-in Event Hub endpoint
  4. Write function code that:
    • Receives telemetry messages from IoT Hub
    • Parses the message to extract temperature and humidity
    • Updates the corresponding digital twin properties using the Azure Digital Twins SDK

Example function code structure:

csharp

// Parse incoming message
var message = JsonConvert.DeserializeObject<SensorData>(messageBody);

// Create digital twin update patch
var updateTwinData = new JsonPatchDocument();
updateTwinData.AppendReplace("/temperature", message.Temperature);
updateTwinData.AppendReplace("/humidity", message.Humidity);

// Update the digital twin
await client.UpdateDigitalTwinAsync(twinId, updateTwinData);

Creating Event Subscription

Connect your IoT Hub to the Azure Function by creating an event subscription:

  1. Navigate to your IoT Hub
  2. Go to Events
  3. Create an event subscription
  4. Select your Azure Function as the endpoint
  5. Choose "Device Telemetry" as the event type

Sending Sensor Data from Raspberry Pi

Create an application on your Raspberry Pi that reads sensor data and publishes it to IoT Hub. This can be implemented in Python, Node-RED, C#, or any language with Azure IoT SDK support.

The application should:

  1. Initialize the DHT11 sensor connection
  2. Read temperature and humidity values periodically
  3. Format values as JSON
  4. Send messages to IoT Hub using the device connection string
  5. Repeat at regular intervals (for example, every 5 seconds)

Example message format:

json

{
 "temperature": 25.4,
 "humidity": 68.2,
 "timestamp": "2021-08-15T10:30:00Z"
}

Verifying the Complete Solution

Once everything is connected:

  1. Start your Raspberry Pi application sending sensor data
  2. Verify messages arrive at IoT Hub (check the metrics in Azure portal)
  3. Check that the Azure Function executes (view function logs)
  4. Open Azure Digital Twins Explorer
  5. Query your digital twin
  6. Observe temperature and humidity properties updating with real sensor values
  7. Interact with the physical sensor (breathe on it to change temperature/humidity) and watch the digital twin reflect those changes within seconds

This demonstrates a complete digital twin solution where physical sensor changes immediately reflect in the digital representation.

Use Cases for Azure Digital Twins

Digital twin solutions enable various scenarios:

Predictive Maintenance: Monitor equipment conditions and predict failures before they occur

Process Optimization: Simulate different operating parameters to find optimal settings

Building Management: Model entire buildings with HVAC, lighting, and occupancy data

Supply Chain Visibility: Track products and materials through manufacturing and distribution

Smart Cities: Model urban infrastructure for traffic management, energy distribution, and public services

Summary

Azure Digital Twins provides a comprehensive platform for creating virtual representations of physical environments. The Digital Twins Definition Language (DTDL) enables structured modeling of objects with properties, telemetry, components, and relationships.

Building a complete solution involves creating models, setting up Azure Digital Twins instances, using Azure Digital Twins Explorer for management, and connecting real devices through IoT Hub and Azure Functions to update twins with live data.

This architecture creates synchronized digital and physical environments, enabling advanced analytics, simulations, and predictions that drive better decision-making in industrial, commercial, and urban applications.