For Googlers looking for a solution after hours of research spl_object_id
is your solution.
Read more here: https://www.php.net/manual/en/function.spl-object-id.php
Example:
<?php
$id = spl_object_id($object);
?>
<div id="carousel<?php echo $id;?> class="carousel">