As automation changes the way factories operate, some U.S. companies are training workers in programming and robotics, letting machinists get a taste of coding.

Competition from China was among the reasons Drew Greenblatt, chief executive of manufacturing firm Marlin Steel Wire Products LLC, purchased $2 million worth of robots in the past 15 months. The Baltimore-based maker of wire baskets is training employees on operating the robots and using laser-cutting software.

The company’s machinists develop code so robots can make parts to specifications, replacing several workers who physically created parts. Other employees use collaborative software to interact with customers on real-time design changes, helping the company manufacture higher-quality steel products, charge more for them and create unique intellectual property, he said.

Marlin Steel is on track to generate $8 million in revenue this year, up from about $5 million the previous year.

Newsletter Sign-up CIO Journal The Morning Download delivers daily insights and news on business technology from the CIO Journal team. PREVIEW

“We’re not going to beat the competition because we are charging lower prices. We are going to beat the competition because of the technology. These are factory workers turning into coders to exploit the technologies,” Mr. Greenblatt said.

The automated factory floor has helped streamline operations and bring in new business. It has also contributed to a technology-based skills gap. A survey released in 2018 by Deloitte and the Manufacturing Institute found that about 2.4 million jobs in the manufacturing sector could remain unfilled between 2018 and 2028. Half of the respondents said they implemented tools like artificial intelligence, robotics, 3-D printing and “the Internet of Things” in manufacturing.

Attracting and retaining high-quality workers was the biggest concern of almost three-quarters of 466 respondents in a National Association of Manufacturers outlook survey released in March.

The U.S. is lagging behind Europe, specifically Germany, in digitizing factories, said Morris Cohen, professor of manufacturing and logistics at the University of Pennsylvania’s Wharton business school. Mr. Cohen cited the example of BMW AG , which involves employees in technology strategies.

“They make a very big deal of bringing workers along—training them, teaching them that this is to their benefit, that this is not a way of replacing you, but making you more productive,” Mr. Cohen said.

The gap between Europe and the U.S. could widen as people who entered the workforce in the 1970s and 1980s begin to retire, said Nicole Radziwill, associate professor of data science and production systems at James Madison University.

Most of the entry- and mid-level manufacturing workers aren’t used to software and interpreting and acting on data, and training in those areas is gaining importance, Ms. Radziwill said.

“The next generation of manufacturing work is all about generating, keeping track, and getting data where it needs to go to keep production processes in control and to capture new opportunities,” she said.

Reskilling requires that manufacturers have a better understanding of their employees.

Radwell International Inc., a manufacturing and repair firm based in Willingboro, N.J., identified workers with an aptitude for learning and decent knowledge of processes and systems and trained them in skills such as programming on Visual Basic to build software tools to handle tasks like purchasing.

Radwell IT staff who learned Python, a programming language used widely in artificial intelligence and data science, built an AI system to sort incoming parts. The system helps recognize parts based on rough contours, differentiating a circuit breaker from a motor. The staff is now developing a machine-vision-based AI system to recognize parts. Employees are also being trained on manufacturing techniques like 3-D printing to make replacement parts for customers.

“We are automating processes through technology, whether it’s AI or system settings like ordering products. We want to retrain them to be valuable employees,” said John Janthor, vice president of information technology at Radwell.

Mr. Janthor said younger workers adjusted well to using technology, with experienced workers needing training to adapt.

“We try to take the tech out of it so they are using tech and not aware of it,” he said.

Write to Agam Shah at agam.shah@wsj.com