November 11, 2025
November 11, 2025

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.
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.
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:
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.
A digital twin model in DTDL is composed of four key elements:
Properties represent the state of the physical entity. Using the compressor example, properties could include:
Properties require a storage mechanism that retains data permanently and allows updates as needed.
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 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 define how your digital twin is related to other digital twins. Examples:
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.
All elements of a digital twin model are enclosed in an interface, which serves as the container for the model definition.
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"
}
]
}
@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
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.
The schema can be primitive types (integer, double, string, boolean) or complex types (object, enum, map).
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"
}
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.
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.
The deployment takes a few minutes. Once complete, note the instance name, hostname, and resource group—you'll need these for connecting client applications.
If using a Microsoft account, set up user access permissions for a corporate account to enable client application access:
This grants permissions to manage the instance, upload models, create twins, and execute queries.
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:
client/src directory in the extracted foldernpm install to install dependenciesnpm run start to launch the applicationThe Explorer opens in your web browser, providing a visual interface for managing your digital twins.
In Azure Digital Twins Explorer:
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.
To update digital twins with real sensor data, create a data pipeline:
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);
Connect your IoT Hub to the Azure Function by creating an event subscription:
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:
Example message format:
json
{
"temperature": 25.4,
"humidity": 68.2,
"timestamp": "2021-08-15T10:30:00Z"
}
Once everything is connected:
This demonstrates a complete digital twin solution where physical sensor changes immediately reflect in the digital representation.
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
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.