Setting entity reference field value in Drupal 8 programatically
If you are creating a node with reference field, see below code:

 use \Drupal\node\Entity\Node;
 $node = Node::create([
   'type' =&gt; 'post',
   'field_post' =&gt; [
     'value' =&gt; 'test',
     'format' =&gt; 'basic_html',
   ],
   'field_pattern' =&gt; [
      'target_id' =&gt; $ID_REFERENCE_ENTITY, // Here just provide referenced entity id.
   ],
   'field_visibility' =&gt; 1, 
   'user_id' =&gt; $uid
 ]);
 $node-&gt;save();


If you are are not creating node then just replace your field_pattern like below:

'field_pattern' =&gt; [
   'target_id' =&gt; $ID_REFERENCE_ENTITY, // Here just provide referenced entity id.
],