Mastering Siemens PLC Memory Arrays

Article summary

Indirect array addressing is a rite of passage for the PLC programmer, as it unlocks higher-level programming such as storing and retrieving large amounts of data. Playing a song is a classic way to learn arrays and indirect memory access. This article presents a PLC implementation allowing the Pulse Train Output on a Siemens S7-1200 PLC to sing through a speaker.

:stopwatch: Estimated reading time: 4 minutes

Introduction to indirect addressing in PLC arrays

An array is an ordered grouping of memory elements where all elements have the same type. In the Programmable Logic Controller (PLC), we define an array by declaring the memory type as well as the size of the array. For instance, we could construct a single-dimension array to hold the barcode information as containers move on a conveyor. In this application, the first barcode would be stored as barCodes[0].

What is indirect addressing?

Array elements may be accessed using direct or indirect methods:

  • Direct: A constant is used to identify the array element of interest e.g., barCodes[1].

  • Indirect: A variable is used to identify the element of interest e.g., barCodes[x] where x is an unsigned integer bounded by the size of the array.

Indirect memory access is an inseparable tool when working with arrays. It allows the programmer to iterate over an array. In the previous example, we could declare an unsigned integer containerCount. We then access the array elements as barCodes[containerCount]. As each container passes, we increment containerCount and then store the barcode.

Indirect addressing is a rite of passage for any programmer. It’s a challenging concept. However, once mastered it opens many possibilities. Our opening barcode is a good example. Another is the ability to capture and store error messages for display on an HMI. With arrays, the technician could scroll through the error messages to determine the root cause of the error.

Indirect addressing demonstrated by playing a song

Playing a song is a classic method used to teach indirect addressing to embedded microcontroller programmers. Arrays are used to hold the musical notes, while a timer is used to advance to the next note.

This same technique can be used for Siemens TIA Portal and the S7-1200 as pictured in Figure 1. While the PLC isn’t typically considered a music box, it does have multiple pulse generators, typically used to drive motors. These generators may be repurposed to drive a speaker, providing music with a 1980’s video game vibe.

To make the PLC sing, we configure the pulse train hardware. The result is included in Video 1 where the PLC plays Scott Joplin’s “The Entertainer” piano rag.

Figure 1: The Siemens S7-1200 PLC is mounted on a Phase Dock trainer.

Author’s reflections (experience)

The Joplin song shown in Video 1 moves fast and contains over 100 notes (including the musical rests). As a lifetime musician, it was easy to identify the notes from the sheet music. The hard part is correctly (manually) converting each note into a frequency. Perhaps I should have written a computer program, but instead I used a spreadsheet.

Video 1: The Siemens S7-1200 “singing” Joplin’s “The Entertainer.”

How to initialize array elements

My technique is described in this article. There you will find a description of the Siemens Data Block (DB) along with the intermediate spreadsheet used to initialize the array elements.

Tech Tip: The Siemens Data Block (DB) provides a portable memory container. This DB object is the native Siemens way to handle memory. As shown in a previous article, combining the DB with a spreadsheet or computer program provides a convenient way to transfer data into the PLC.

How to control the Siemens Pulse generators (PTO and PWM)

The Siemens S7-1200 includes pulse generators that can be configured for Pulse Train Output (PTO) or Pulse Width Modulation (PWM). In this application, we use the PTO as it allows dynamic frequency adjustment while maintaining a 50% duty cycle. The instantiation is shown in Figure 2. There we observe the indirect addressing of musicalNotesDatBlock.note[#uiIndex]. Note that uiIndex is a pointer to the next note while a rising edge on xLoadNote will transfer the note to the PTO block.

Please refer to this link for instructions to configure the Siemens PTO.

The code for the music UDFB may be downloaded in pdf form. It’s a relatively straightforward approach to generate a pulse every timNote seconds. This pulse is used to increment uiIndex and to load a new value in the PTO block. Finally, a guard is used to reset uiIndex at the end of the song.

playSong.pdf (75.7 KB)
MusicalNotesDataBlock.db (3.2 KB)

Figure 2: Instantiation of the Siemens PTO hardware to play the musical notes.

Speaker connection

A small speaker was used as shown in Video 1. A series current resistor is used to both protect both the PLC’s solid-state output and the speaker. As a starting point, use a 1 kΩ resistor and adjust as necessary to control the volume.

Next steps

This article provides a working example of indirect array addressing. You are encouraged to experiment:

  • Change the song
  • Use a multidimensional array to hold both note and duration.
  • Instantiate another PTO to play a countermelody
  • Use the indirect addressing technique to construct a lookup table simulating the operation of an industrial clothes washer
  • Construct a Simon Says game using indirect addressing to play the moves and track the player’s response
  • Use arrays to record robot arm positions for playback.

Parting thoughts

PLC programming concepts don’t need to be abstract. Programming music is a very good exercise that sets the stage for future learning. Your students will have a solid understanding of arrays, and they will be ready to step to motor drives (excuse the pun).
Please give this technique a try and share your observations in the comments section.
Best wishes,

APDahlen

Related articles by this author

If you enjoyed this article, you may also find these related articles helpful:

TL;DR

  • Arrays and indirect addressing are a gateway to advanced PLC programming. They allow us to store and access lists of items.
  • Array manipulation to play a song is a fun activity with immediate feedback for the programmer.
  • The PLC Pulse Train Output (PTO) hardware, typically used to command a motor driver, can play music through a speaker.
  • The song is held in an array, with indirect addressing used to select the specific note being played.

About this author

Aaron Dahlen, LCDR USCG (Ret.), serves as an application engineer at DigiKey. He has a unique electronics and automation foundation built over a 27-year military career as a technician and engineer which was further enhanced by 12 years of teaching (interwoven). With an MSEE degree from Minnesota State University, Mankato, Dahlen has taught in an ABET-accredited EE program, served as the program coordinator for an EET program, and taught component-level repair to military electronics technicians. Dahlen has returned to his Northern Minnesota home and thoroughly enjoys researching and writing articles such as this.