My theory on why North Korea has stepped up its provocation of South Korea since their nuclear missle tests is that they see this as a tug-of-war.
Suppose that North Korea wants to keep its nuclear weapons program. If they hadn't sunk a ship and bombed a city, world leaders would currently be pressuring North Korea to stop making nuclear weapons. Instead, they're pressuring North Korea to stop doing something (make provocative attacks) that North Korea doesn't really want to do anyway. And when North Korea (temporarily) stops attacking South Korea, everybody can go home and say they "did something about North Korea". And North Korea can keep on making nukes.
That would have been a likely scenario during the Cold War, when people on both sides were really afraid of a nuclear attack. But neither a ship, nor an island, looks like an incoming nuclear attack; nor is South Korea supposed to have nuclear weapons AFAIK; and if the North Korean soldiers have any sense they would be much more afraid of their own superiors than of South Korea. And in a dictatorship where you need permission for just about everything you do, I doubt that a low-ranking military commander could sink a ship, or start an artillery barrage on another country, on his (her? any women in the N Korean military?) own initiative.
If they have standing orders like "If you're shot at, shoot back" then there's still potential for trouble. I think that, in the recent incident, South Korea was holding military exercises in or around places that North Korea claims are part of its territory (claims which the rest of the world doesn't recognize), and the exercises included things like shooting at targets. So it doesn't seem that weird for some North Korean guy to go "OMG, they're shooting at us!" when the South Korea military is nearby and shooting.